Skip to content

Commit

Permalink
Tests: merge common tests for all platforms and improve Linux' ones
Browse files Browse the repository at this point in the history
Merge macOS and common tests.
Linux: improve and add testcases to honor .appName and cleanup behind us.

Signed-off-by: Alexandre Demers <[email protected]>
  • Loading branch information
Oxalin committed Jan 18, 2024
1 parent f3a992a commit 370d4f7
Showing 1 changed file with 121 additions and 137 deletions.
258 changes: 121 additions & 137 deletions tests/index.coffee
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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'
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
Expand All @@ -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

0 comments on commit 370d4f7

Please sign in to comment.