From 370d4f711d1272ff4169a3e9fb2acc7dfdcd250e Mon Sep 17 00:00:00 2001 From: Alexandre Demers Date: Thu, 18 Jan 2024 00:45:43 -0500 Subject: [PATCH] Tests: merge common tests for all platforms and improve Linux' ones Merge macOS and common tests. Linux: improve and add testcases to honor .appName and cleanup behind us. Signed-off-by: Alexandre Demers --- tests/index.coffee | 258 +++++++++++++++++++++------------------------ 1 file changed, 121 insertions(+), 137 deletions(-) diff --git a/tests/index.coffee b/tests/index.coffee index 9fb9890..408236d 100644 --- a/tests/index.coffee +++ b/tests/index.coffee @@ -1,10 +1,13 @@ chai = require 'chai' +fs = require 'fs' path = require 'path' +untildify = require 'untildify' expect = chai.expect AutoLaunch = require '../src/' AutoLaunchHelper = require './helper' isMac = false +followsXDG = false if /^win/.test process.platform executablePath = path.resolve path.join './tests/executables', 'GitHubSetup.exe' @@ -12,7 +15,7 @@ else if /darwin/.test process.platform isMac = true executablePath = '/Applications/Calculator.app' else if (/linux/.test process.platform) or (/freebsd/.test process.platform) - isLinux = true + followsXDG = true executablePath = path.resolve path.join './tests/executables', 'hv3-linux-x86' console.log "Executable being used for tests:", executablePath @@ -22,165 +25,146 @@ describe 'node-auto-launch', -> autoLaunch = null autoLaunchHelper = null - beforeEach -> autoLaunch = new AutoLaunch name: 'node-auto-launch test' path: executablePath + mac: + useLaunchAgent: if isMac then true autoLaunchHelper = new AutoLaunchHelper(autoLaunch) - if not isMac - describe '.isEnabled', -> - beforeEach -> - autoLaunchHelper.ensureDisabled() - - it 'should be disabled', (done) -> - autoLaunch.isEnabled().then (enabled) -> - expect(enabled).to.equal false - done() - .catch done - return - - it 'should catch errors', (done) -> - autoLaunchHelper.mockApi - isEnabled: -> - Promise.reject() - - autoLaunch.isEnabled().catch done - return - - - describe '.enable', -> - beforeEach -> - autoLaunchHelper.ensureDisabled() - - it 'should enable auto launch', (done) -> - autoLaunch.enable() - .then () -> - autoLaunch.isEnabled() - .then (enabled) -> - expect(enabled).to.equal true - done() - .catch done - return - - it 'should catch errors', (done) -> - autoLaunchHelper.mockApi - enable: -> Promise.reject() + describe '.isEnabled', -> + beforeEach -> + autoLaunchHelper.ensureDisabled() - autoLaunch.enable().catch done - return + it 'should be disabled', (done) -> + autoLaunch.isEnabled().then (enabled) -> + expect(enabled).to.equal false + done() + .catch done + return + it 'should catch errors', (done) -> + autoLaunchHelper.mockApi + isEnabled: -> + Promise.reject() - describe '.disable', -> - beforeEach -> - autoLaunchHelper.ensureEnabled() + autoLaunch.isEnabled().catch done + return - it 'should disable auto launch', (done) -> - autoLaunch.disable() - .then -> autoLaunch.isEnabled() - .then (enabled) -> - expect(enabled).to.equal false - done() - .catch done - return + describe '.enable', -> + beforeEach -> + autoLaunchHelper.ensureDisabled() + + it 'should enable auto launch', (done) -> + autoLaunch.enable() + .then () -> + autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal true + done() + .catch done + return + + it 'should catch errors', (done) -> + autoLaunchHelper.mockApi + enable: -> Promise.reject() + + autoLaunch.enable().catch done + return + + describe '.disable', -> + beforeEach -> + autoLaunchHelper.ensureEnabled() - it 'should catch errors', (done) -> - autoLaunchHelper.mockApi - disable: -> - Promise.reject() + it 'should disable auto launch', (done) -> + autoLaunch.disable() + .then -> autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal false + done() + .catch done + return - autoLaunch.disable().catch done - return + it 'should catch errors', (done) -> + autoLaunchHelper.mockApi + disable: -> + Promise.reject() + autoLaunch.disable().catch done + return - if isLinux - describe '.appName', -> - it 'should honor name option', (done) -> - expect(autoLaunch.opts.appName).to.equal 'node-auto-launch test' - done() - return + return unless followsXDG - describe 'testing path name', -> - executablePathLinux = path.resolve './path with spaces/' - autoLaunchLinux = new AutoLaunch - name: 'node-auto-launch test' - path: executablePathLinux - autoLaunchHelper = new AutoLaunchHelper(autoLaunchLinux) + describe 'testing .appName', -> + beforeEach -> + autoLaunchLinux = null + autoLaunchHelper = null - it 'should properly escape reserved caracters', (done) -> - expect(autoLaunchLinux.opts.appPath).not.to.equal executablePathLinux + it 'without space', (done) -> + autoLaunchLinux = new AutoLaunch + name: 'node-auto-launch' + path: executablePath + autoLaunchHelper = new AutoLaunchHelper(autoLaunchLinux) + + autoLaunchLinux.enable() + .then () -> + desktopEntryPath = untildify(path.join('~/.config/autostart/', autoLaunchLinux.opts.appName + '.desktop')) + fs.stat desktopEntryPath, (err, stats) => + if err + done err + expect(stats.isFile()).to.equal true done() - return + .catch done + return - - # Let's test some Mac-only options - return unless isMac - - describe 'mac.useLaunchAgent', -> - autoLaunchWithLaunchAgent = null - autoLaunchWithLaunchAgentHelper = null - - beforeEach -> - autoLaunchWithLaunchAgent = new AutoLaunch + it 'with space', (done) -> + autoLaunchLinux = new AutoLaunch name: 'node-auto-launch test' path: executablePath - mac: - useLaunchAgent: true - autoLaunchWithLaunchAgentHelper = new AutoLaunchHelper autoLaunchWithLaunchAgent - - describe '.isEnabled', -> - beforeEach -> autoLaunchWithLaunchAgentHelper.ensureDisabled() - - it 'should be disabled', (done) -> - autoLaunchWithLaunchAgent.isEnabled().then (enabled) -> - expect(enabled).to.equal false + autoLaunchHelper = new AutoLaunchHelper(autoLaunchLinux) + + autoLaunchLinux.enable() + .then () -> + desktopEntryPath = untildify(path.join('~/.config/autostart/', autoLaunchLinux.opts.appName + '.desktop')) + fs.stat desktopEntryPath, (err, stats) => + if err + done err + expect(stats.isFile()).to.equal true done() - .catch done - return - - it 'should catch errors', (done) -> - autoLaunchWithLaunchAgentHelper.mockApi - isEnabled: -> Promise.reject() - - autoLaunchWithLaunchAgent.isEnabled().catch done - return - - - describe '.enable', -> - beforeEach -> autoLaunchWithLaunchAgentHelper.ensureDisabled() - - it 'should enable auto launch', (done) -> - autoLaunchWithLaunchAgent.enable().then -> - autoLaunchWithLaunchAgent.isEnabled().then (enabled) -> - expect(enabled).to.equal true - done() - .catch done - return - - it 'should catch errors', (done) -> - autoLaunchWithLaunchAgentHelper.mockApi - enable: -> Promise.reject() - - autoLaunchWithLaunchAgent.enable().catch done - return + .catch done + return + it 'with capital letters', (done) -> + autoLaunchLinux = new AutoLaunch + name: 'Node-Auto-Launch' + path: executablePath + autoLaunchHelper = new AutoLaunchHelper(autoLaunchLinux) + + autoLaunchLinux.enable() + .then () -> + desktopEntryPath = untildify(path.join('~/.config/autostart/', autoLaunchLinux.opts.appName + '.desktop')) + fs.stat desktopEntryPath, (err, stats) => + if err + done err + expect(stats.isFile()).to.equal true + done() + .catch done + return - describe '.disable', -> - beforeEach -> autoLaunchWithLaunchAgentHelper.ensureEnabled() + afterEach -> + autoLaunchHelper.ensureDisabled() - it 'should disable auto launch', (done) -> - autoLaunchWithLaunchAgent.disable() - .then -> autoLaunchWithLaunchAgent.isEnabled() - .then (enabled) -> - expect(enabled).to.equal false - done() - .catch done - return + describe 'testing path name', -> + executablePathLinux = path.resolve './path with spaces/' + autoLaunchLinux = new AutoLaunch + name: 'node-auto-launch test' + path: executablePathLinux + autoLaunchHelper = new AutoLaunchHelper(autoLaunchLinux) - it 'should catch errors', (done) -> - autoLaunchWithLaunchAgentHelper.mockApi - disable: -> Promise.reject() + it 'should properly escape reserved caracters', (done) -> + expect(autoLaunchLinux.opts.appPath).not.to.equal executablePathLinux + done() + return - autoLaunchWithLaunchAgent.disable().catch done - return + return