From fb14f953cd6c1a819da00d19be6fef178655bbb7 Mon Sep 17 00:00:00 2001 From: Charlie Powell <31997505+cp2004@users.noreply.github.com> Date: Thu, 11 Aug 2022 14:57:28 +0100 Subject: [PATCH 1/3] :bug: Fix spinner icon on file delete (#4616) --- src/octoprint/static/js/app/viewmodels/files.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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" ); }; } From 9f1ffde4c07e2515bde6c0c03edb8eb6c23ae16e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 11 Aug 2022 16:23:16 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=85=20e2e:=20Make=20port=20configurab?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cookie are port specific, so they need to be determine from the port from the base URL. --- tests/cypress/integration/login.js | 16 +++++++++++----- tests/cypress/util/util.js | 6 ++++++ 2 files changed, 17 insertions(+), 5 deletions(-) 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}`; +}; From b4c55fdf560ed1cc096faf6ffc4751c4b67f1deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 11 Aug 2022 17:50:24 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=A8=20Add=20octoprint.util.files.sear?= =?UTF-8?q?ch=5Fin=5Ffile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uses native grep if available, otherwise falls back to a python implementation. Migrated from FileCheck plugin. --- src/octoprint/util/files.py | 45 +++++++++++++++++++++++++++++++++++++ tests/util/test_files.py | 35 ++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) 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/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)