Skip to content

Commit

Permalink
Adding conditional binding so that we can retain type-safety of the i…
Browse files Browse the repository at this point in the history
…mport for things like `permissions.AuthType` as function params with a generic import that is platform-agnostic. Enhancement and fixes: #23

Adding additional test to run on a separate linux node
  • Loading branch information
Mike Maietta committed Mar 14, 2021
1 parent d17c249 commit 92e80cd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
version: 14.x
node-version: 14.x
- name: Lint
run: |
npm install
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ on:

jobs:
build:
runs-on: macOS-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
steps:
- uses: actions/checkout@master
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
version: 14.x
- name: npm install, build, and test
node-version: 14.x
- name: npm install, build, and test (${{ matrix.os }})
run: |
npm install
npm test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ This native Node.js module allows you to manage an app's access to:
* Speech Recognition
* Protected Folders

Note: Always will return `undefined` when imported on a non-Mac platform

## API

## `permissions.getAuthStatus(type)`
Expand Down
30 changes: 19 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
const permissions = require('bindings')('permissions.node')
const nonMacResponse = () => undefined
const stub = {
askForCalendarAccess: nonMacResponse,
askForContactsAccess: nonMacResponse,
askForFoldersAccess: nonMacResponse,
askForFullDiskAccess: nonMacResponse,
askForRemindersAccess: nonMacResponse,
askForCameraAccess: nonMacResponse,
askForMicrophoneAccess: nonMacResponse,
askForPhotosAccess: nonMacResponse,
askForSpeechRecognitionAccess: nonMacResponse,
askForScreenCaptureAccess: nonMacResponse,
askForAccessibilityAccess: nonMacResponse,
getAuthStatus: nonMacResponse,
}

const os = require('os')
const permissions = os.platform() === 'darwin' ? require('bindings')('permissions.node') : stub

function getAuthStatus(type) {
const validTypes = [
Expand Down Expand Up @@ -34,16 +51,7 @@ function askForFoldersAccess(folder) {
}

module.exports = {
askForCalendarAccess: permissions.askForCalendarAccess,
askForContactsAccess: permissions.askForContactsAccess,
...permissions,
askForFoldersAccess,
askForFullDiskAccess: permissions.askForFullDiskAccess,
askForRemindersAccess: permissions.askForRemindersAccess,
askForCameraAccess: permissions.askForCameraAccess,
askForMicrophoneAccess: permissions.askForMicrophoneAccess,
askForPhotosAccess: permissions.askForPhotosAccess,
askForSpeechRecognitionAccess: permissions.askForSpeechRecognitionAccess,
askForScreenCaptureAccess: permissions.askForScreenCaptureAccess,
askForAccessibilityAccess: permissions.askForAccessibilityAccess,
getAuthStatus,
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"build": "node-gyp rebuild",
"clean": "node-gyp clean",
"lint": "prettier --check index.js",
"format": "clang-format -i permissions.mm && prettier --write index.js",
"lint": "prettier --check '**/*.js'",
"format": "clang-format -i permissions.mm && prettier --write '**/*.js'",
"test": "./node_modules/.bin/mocha --reporter spec"
},
"repository": {
Expand Down
45 changes: 36 additions & 9 deletions test/module.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const { expect } = require('chai')
const {
askForFoldersAccess,
getAuthStatus,
} = require('../index')
const permissions = require('../index')

const { platform } = require('os')
const isMac = platform() === 'darwin'
it.ifMac = isMac ? it : it.skip
it.ifNotMac = isMac ? it.skip : it

describe('node-mac-permissions', () => {
describe('getAuthStatus()', () => {
it('should throw on invalid types', () => {
expect(() => {
getAuthStatus('bad-type')
permissions.getAuthStatus('bad-type')
}).to.throw(/bad-type is not a valid type/)
})

it('should return a string', () => {
it.ifMac('should return a string', () => {
const types = [
'contacts',
'calendar',
Expand All @@ -24,12 +26,12 @@ describe('node-mac-permissions', () => {
'microphone',
'accessibility',
'location',
'screen'
'screen',
]

const statuses = ['not determined', 'denied', 'authorized', 'restricted']
for (const type of types) {
const status = getAuthStatus(type)
const status = permissions.getAuthStatus(type)
expect(statuses).to.contain(status)
}
})
Expand All @@ -38,8 +40,33 @@ describe('node-mac-permissions', () => {
describe('askForFoldersAccess()', () => {
it('should throw on invalid types', () => {
expect(() => {
askForFoldersAccess('bad-type')
permissions.askForFoldersAccess('bad-type')
}).to.throw(/bad-type is not a valid protected folder/)
})
})

describe('conditional binding', () => {
it.ifNotMac('always return undefined for non-mac OS', async () => {
const asyncModuleExports = [
'askForCalendarAccess',
'askForContactsAccess',
'askForFullDiskAccess',
'askForRemindersAccess',
'askForCameraAccess',
'askForMicrophoneAccess',
'askForPhotosAccess',
'askForSpeechRecognitionAccess',
'askForScreenCaptureAccess',
'askForAccessibilityAccess',
]

for (const func of asyncModuleExports) {
const auth = await permissions[func]()
expect(auth).to.be.undefined
}

expect(permissions.getAuthStatus('contacts')).to.be.undefined
expect(permissions.askForFoldersAccess('desktop')).to.be.undefined
})
})
})

0 comments on commit 92e80cd

Please sign in to comment.