diff --git a/src/octoprint/static/js/app/viewmodels/files.js b/src/octoprint/static/js/app/viewmodels/files.js index b0d4c4f15..1c924ecf1 100644 --- a/src/octoprint/static/js/app/viewmodels/files.js +++ b/src/octoprint/static/js/app/viewmodels/files.js @@ -861,13 +861,13 @@ $(function () { var icon = $("i.fa-trash-alt", element); if (icon.length) { activateSpinner = function () { - icon.removeClass("fa-trash-alt").addClass( - "fa-spinner fa-spin" + icon.removeClass("far fa-trash-alt").addClass( + "fas fa-spinner fa-spin" ); }; finishSpinner = function () { - icon.removeClass("fa-spinner fa-spin").addClass( - "fa-trash-alt" + icon.removeClass("fas fa-spinner fa-spin").addClass( + "far fa-trash-alt" ); }; } diff --git a/src/octoprint/util/files.py b/src/octoprint/util/files.py index cd3a4b404..62c5cb820 100644 --- a/src/octoprint/util/files.py +++ b/src/octoprint/util/files.py @@ -2,6 +2,7 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agp __copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License" import itertools +import logging import os.path import re @@ -270,3 +271,47 @@ def silent_remove(file): os.remove(file) except OSError: pass + + +def search_through_file(path, term, regex=False): + if regex: + pattern = term + else: + pattern = re.escape(term) + compiled = re.compile(pattern) + + logger = logging.getLogger(__name__) + + try: + try: + # try native grep + import sarge + + result = sarge.capture_stderr(["grep", "-q", "-E", pattern, path]) + if result.stderr.text: + logger.warning( + "Error raised by native grep, falling back to python " + "implementation: {}".format(result.stderr.text.strip()) + ) + return search_through_file_python(path, term, compiled) + return result.returncode == 0 + except ValueError as exc: + if "Command not found" in str(exc): + return search_through_file_python(path, term, compiled) + else: + raise + except Exception: + logger.exception( + "Something unexpectedly went wrong while trying to " + "search for {} in {} via grep".format(term, path) + ) + + return False + + +def search_through_file_python(path, term, compiled): + with open(path, encoding="utf8", errors="replace") as f: + for line in f: + if term in line or compiled.match(term): + return True + return False diff --git a/tests/cypress/integration/login.js b/tests/cypress/integration/login.js index afe7307fa..1281ffd99 100644 --- a/tests/cypress/integration/login.js +++ b/tests/cypress/integration/login.js @@ -1,4 +1,10 @@ -import {prepare_server, await_coreui, await_loginui, login} from "../util/util"; +import { + prepare_server, + await_coreui, + await_loginui, + login, + get_cookie_name +} from "../util/util"; context("Login tests", () => { const username = "admin"; @@ -27,8 +33,8 @@ context("Login tests", () => { await_coreui(); cy.get("[data-test-id=login-menu-title]").should("contain", username); - cy.getCookie("session_P5000").should("exist"); - cy.getCookie("remember_token_P5000").should("not.exist"); + cy.getCookie(get_cookie_name("session")).should("exist"); + cy.getCookie(get_cookie_name("remember_token")).should("not.exist"); cy.location().should((loc) => { expect(loc.hash).to.eq("#temp"); }); @@ -45,8 +51,8 @@ context("Login tests", () => { await_coreui(); cy.get("[data-test-id=login-menu-title]").should("contain", username); - cy.getCookie("session_P5000").should("exist"); - cy.getCookie("remember_token_P5000").should(($cookie) => { + cy.getCookie(get_cookie_name("session")).should("exist"); + cy.getCookie(get_cookie_name("remember_token")).should(($cookie) => { expect($cookie).to.have.property("value"); expect($cookie.value).to.match(new RegExp("^" + username + "|")); }); diff --git a/tests/cypress/util/util.js b/tests/cypress/util/util.js index 81e769ad8..2b568e660 100644 --- a/tests/cypress/util/util.js +++ b/tests/cypress/util/util.js @@ -70,3 +70,9 @@ export const ensure_file_unknown = (location, name) => { failOnStatusCode: false }); }; + +export const get_cookie_name = (cookie) => { + const url = new URL(Cypress.config("baseUrl")); + const port = url.port || (url.protocol === "https:" ? 443 : 80); + return `${cookie}_P${port}`; +}; diff --git a/tests/util/test_files.py b/tests/util/test_files.py index 5b2c09c77..272f39972 100644 --- a/tests/util/test_files.py +++ b/tests/util/test_files.py @@ -1,11 +1,17 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License" +import os +import re import unittest from ddt import data, ddt, unpack -from octoprint.util.files import sanitize_filename +from octoprint.util.files import ( + sanitize_filename, + search_through_file, + search_through_file_python, +) @ddt @@ -33,3 +39,30 @@ class FilesUtilTest(unittest.TestCase): self.fail("expected ValueError") except ValueError as ex: self.assertEqual(str(ex), "name must not contain / or \\") + + @data( + ("umlaut", False, True), + ("BOM", False, True), + (r"^[^#]*BOM", True, False), + ) + @unpack + def test_search_through_file(self, term, regex, expected): + path = os.path.join( + os.path.abspath(os.path.dirname(__file__)), "_files", "utf8_without_bom.txt" + ) + actual = search_through_file(path, term, regex=regex) + self.assertEqual(actual, expected) + + @data( + ("umlaut", True), + ("BOM", True), + (r"^[^#]*BOM", False), + ) + @unpack + def test_search_through_file_python(self, term, expected): + compiled = re.compile(term) + path = os.path.join( + os.path.abspath(os.path.dirname(__file__)), "_files", "utf8_without_bom.txt" + ) + actual = search_through_file_python(path, term, compiled) + self.assertEqual(actual, expected)