From bcb9a2d0d724dcc9a0369e37d7816f967385eda5 Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Thu, 12 Dec 2024 19:01:49 -0800 Subject: [PATCH 1/2] Fix `package-keymap-view` compatibility check This PR also makes this compatibility check testable, and even expandable, by being able to provide a new platform if preferred. --- .../settings-view/lib/package-keymap-view.js | 12 +++++-- .../spec/package-keymap-view-spec.js | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 packages/settings-view/spec/package-keymap-view-spec.js diff --git a/packages/settings-view/lib/package-keymap-view.js b/packages/settings-view/lib/package-keymap-view.js index 47236b0f8a..1b7a0e8097 100644 --- a/packages/settings-view/lib/package-keymap-view.js +++ b/packages/settings-view/lib/package-keymap-view.js @@ -11,7 +11,6 @@ import KeybindingsPanel from './keybindings-panel' export default class PackageKeymapView { constructor (pack) { this.pack = pack - this.otherPlatformPattern = new RegExp(`\\.platform-(?!${_.escapeRegExp(process.platform)}\\b)`) this.namespace = this.pack.name this.disposables = new CompositeDisposable() etch.initialize(this) @@ -128,8 +127,8 @@ export default class PackageKeymapView { continue } - if (this.otherPlatformPattern.test(selector)) { - continue + if (this.selectorIsNotCompatibleWithPlatform(selector)) { + continue; } const keyBindingRow = document.createElement('tr') @@ -183,4 +182,11 @@ export default class PackageKeymapView { atom.clipboard.write(content) } + + selectorIsNotCompatibleWithPlatform(selector, platform = process.platform) { + const otherPlatformPattern = new RegExp(`\\.platform-(?!${_.escapeRegExp(platform)}\\b)`); + const currentPlatformPattern = new RegExp(`\\.platform-(${_.escapeRegExp(platform)}\\b)`); + + return otherPlatformPattern.test(selector) && !currentPlatformPattern.test(selector); + } } diff --git a/packages/settings-view/spec/package-keymap-view-spec.js b/packages/settings-view/spec/package-keymap-view-spec.js new file mode 100644 index 0000000000..bd02446596 --- /dev/null +++ b/packages/settings-view/spec/package-keymap-view-spec.js @@ -0,0 +1,35 @@ + +const PackageKeymapView = require("../lib/package-keymap-view.js"); +let view; + +describe("PackageKeymapView", () => { + + beforeEach(() => { + // Just prevent this stuff from calling through, it doesn't matter for this test + spyOn(atom.packages, "getLoadedPackage").andReturn({ keymaps: [] }); + + view = new PackageKeymapView({ + name: "test-package" + }); + }); + + it("should say a selector with no platform listed is compatible with the current one", () => { + expect(view.selectorIsNotCompatibleWithPlatform("atom-text-editor", "win32")).toBe(false); + }); + + it("should say a selector with a platform other than the current is not compatible", () => { + expect(view.selectorIsNotCompatibleWithPlatform(".platform-darwin", "linux")).toBe(true); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-win32", "darwin")).toBe(true); + }); + + it("should say a selector with the current platform listed is compatible", () => { + expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux", "linux")).toBe(false); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-win32", "win32")).toBe(false); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-darwin", "darwin")).toBe(false); + }); + + it("should say a selector with the current platform and others listed is compatible", () => { + expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux, .platform-win32", "win32")).toBe(false); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux, .platform-win32", "linux")).toBe(false); + }); +}); From d624d2601bbd81d0bc120878b77dc21536d02ad8 Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Thu, 12 Dec 2024 21:53:49 -0800 Subject: [PATCH 2/2] Rename function to match expectations. Flip all boolean logic --- .../settings-view/lib/package-keymap-view.js | 6 +++--- .../spec/package-keymap-view-spec.js | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/settings-view/lib/package-keymap-view.js b/packages/settings-view/lib/package-keymap-view.js index 1b7a0e8097..b1938727ca 100644 --- a/packages/settings-view/lib/package-keymap-view.js +++ b/packages/settings-view/lib/package-keymap-view.js @@ -127,7 +127,7 @@ export default class PackageKeymapView { continue } - if (this.selectorIsNotCompatibleWithPlatform(selector)) { + if (!this.selectorIsCompatibleWithPlatform(selector)) { continue; } @@ -183,10 +183,10 @@ export default class PackageKeymapView { atom.clipboard.write(content) } - selectorIsNotCompatibleWithPlatform(selector, platform = process.platform) { + selectorIsCompatibleWithPlatform(selector, platform = process.platform) { const otherPlatformPattern = new RegExp(`\\.platform-(?!${_.escapeRegExp(platform)}\\b)`); const currentPlatformPattern = new RegExp(`\\.platform-(${_.escapeRegExp(platform)}\\b)`); - return otherPlatformPattern.test(selector) && !currentPlatformPattern.test(selector); + return !(otherPlatformPattern.test(selector) && !currentPlatformPattern.test(selector)); } } diff --git a/packages/settings-view/spec/package-keymap-view-spec.js b/packages/settings-view/spec/package-keymap-view-spec.js index bd02446596..0f8e1a60bf 100644 --- a/packages/settings-view/spec/package-keymap-view-spec.js +++ b/packages/settings-view/spec/package-keymap-view-spec.js @@ -14,22 +14,22 @@ describe("PackageKeymapView", () => { }); it("should say a selector with no platform listed is compatible with the current one", () => { - expect(view.selectorIsNotCompatibleWithPlatform("atom-text-editor", "win32")).toBe(false); + expect(view.selectorIsCompatibleWithPlatform("atom-text-editor", "win32")).toBe(true); }); it("should say a selector with a platform other than the current is not compatible", () => { - expect(view.selectorIsNotCompatibleWithPlatform(".platform-darwin", "linux")).toBe(true); - expect(view.selectorIsNotCompatibleWithPlatform(".platform-win32", "darwin")).toBe(true); + expect(view.selectorIsCompatibleWithPlatform(".platform-darwin", "linux")).toBe(false); + expect(view.selectorIsCompatibleWithPlatform(".platform-win32", "darwin")).toBe(false); }); it("should say a selector with the current platform listed is compatible", () => { - expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux", "linux")).toBe(false); - expect(view.selectorIsNotCompatibleWithPlatform(".platform-win32", "win32")).toBe(false); - expect(view.selectorIsNotCompatibleWithPlatform(".platform-darwin", "darwin")).toBe(false); + expect(view.selectorIsCompatibleWithPlatform(".platform-linux", "linux")).toBe(true); + expect(view.selectorIsCompatibleWithPlatform(".platform-win32", "win32")).toBe(true); + expect(view.selectorIsCompatibleWithPlatform(".platform-darwin", "darwin")).toBe(true); }); it("should say a selector with the current platform and others listed is compatible", () => { - expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux, .platform-win32", "win32")).toBe(false); - expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux, .platform-win32", "linux")).toBe(false); + expect(view.selectorIsCompatibleWithPlatform(".platform-linux, .platform-win32", "win32")).toBe(true); + expect(view.selectorIsCompatibleWithPlatform(".platform-linux, .platform-win32", "linux")).toBe(true); }); });