Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #70

Merged
merged 14 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [ 14.x, 16.x, 18.x ]
node-version: [ 14.x, 16.x, 18.x, 20.x ]
os: [ windows-latest, ubuntu-latest, macOS-latest ]

# Go
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dotenv": "~16.3.1",
"eslint": "~8.55.0",
"mock-fs": "~5.2.0",
"mock-tmp": "~0.0.2",
"nyc": "~15.1.0",
"tap-arc": "^1.2.2",
"tape": "^5.7.2"
Expand Down
4 changes: 2 additions & 2 deletions src/config/pragmas/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ module.exports = function configureShared ({ arc, pragmas, inventory, errors })
}
else if (foundPluginSrc) {
if (!required) {
if (!is.exists(shared.src)) shared.src = src
if (!is.exists(shared.src)) return null
if (!is.exists(shared.src) && !is.exists(join(cwd, shared.src))) shared.src = src
if (!is.exists(shared.src) && !is.exists(join(cwd, shared.src))) return null
}
validate.shared(shared.src, cwd, errors, required)
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/pragmas/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ module.exports = function configureViews ({ arc, pragmas, inventory, errors }) {
}
else if (foundPluginSrc) {
if (!required) {
if (!is.exists(views.src)) views.src = src
if (!is.exists(views.src)) return null
if (!is.exists(views.src) && !is.exists(join(cwd, views.src))) views.src = src
if (!is.exists(views.src) && !is.exists(join(cwd, views.src))) return null
}
validate.shared(views.src, cwd, errors, required)
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/project/prefs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ let read = require('../../../read')
let validate = require('../validate')
let { is, validationPatterns: valid } = require('../../../lib')
let { parse } = require('./dotenv')
let { homedir } = require('os')
let os = require('os')

module.exports = function getPrefs ({ scope, inventory, errors }) {
let cwd = scope === 'global'
? homedir()
? os.homedir()
: inventory._project.cwd

let envFilepath = join(cwd, '.env')
Expand Down
11 changes: 7 additions & 4 deletions src/lib/asap-src.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
let { join } = require('path')
let { existsSync } = require('fs')

module.exports = function asapSrc () {
module.exports = function asapSrc (params = {}) {
let { _testing } = params
let dirname = _testing ? _testing : __dirname
// Inventory running as an arc/arc dependency (most common use case)
let src = join(process.cwd(), 'node_modules', '@architect', 'asap', 'src')
if (existsSync(src)) return src

// Inventory running in arc/arc as a global install
let global = join(__dirname, '..', '..', '..', 'asap', 'src')
let global = join(dirname, '..', '..', '..', 'asap', 'src')
if (existsSync(global)) return global

// Inventory running from a local (symlink) context (usually testing/dev)
let local = join(__dirname, '..', '..', 'node_modules', '@architect', 'asap', 'src')
let local = join(dirname, '..', '..', 'node_modules', '@architect', 'asap', 'src')
if (existsSync(local)) return local

try {
return require.resolve('@architect/asap')
}
catch (err) {
catch {
/* istanbul ignore next */
throw Error('Cannot find ASAP module!')
}
}
1 change: 0 additions & 1 deletion src/validate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ module.exports = function finalValidation (params, inventory) {
if (errors.length) {
return errorFmt({ type: 'file path', errors })
}

}
64 changes: 26 additions & 38 deletions test/integration/preferences-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
let { join } = require('path')
let { homedir } = require('os')
let test = require('tape')
let sut = join(process.cwd(), 'src', 'index')
let mockTmp = require('mock-tmp')
let cwd = process.cwd()
let testLibPath = join(cwd, 'test', 'lib')
let { overrideHomedir } = require(testLibPath)
let sut = join(cwd, 'src', 'index')
let inv = require(sut)
let mockFs = require('mock-fs')

let mock = join(process.cwd(), 'test', 'mock')
let arc = '@app\nappname\n@events\nan-event' // Not using @http so we can skip ASAP filesystem checks
let reset = () => mockFs.restore()
let mock = join(cwd, 'test', 'mock')
let globalPrefsFile = '.prefs.arc'
let reset = () => {
mockTmp.reset()
overrideHomedir.reset()
}

/**
* Duplicates some unit tests as part of the larger whole integration test
Expand All @@ -20,6 +25,7 @@ test('Set up env', t => {

test('Get global preferences', t => {
t.plan(11)
let cwd = join(mock, 'prefs', 'global')
let prefs = {
sandbox: { environment: 'testing' },
'sandbox-startup': [
Expand Down Expand Up @@ -55,15 +61,13 @@ testing
env_var_1 foo
env_var_2 bar
`
let path = join(homedir(), '.prefs.arc')
mockFs({
'app.arc': arc,
[path]: prefsText
let tmp = mockTmp({
[globalPrefsFile]: prefsText
})
inv({}, (err, result) => {
overrideHomedir(tmp)
inv({ cwd }, (err, result) => {
if (err) t.fail(err)
else {
mockFs.restore()
let { inv, get } = result
t.ok(inv, 'Inventory returned inventory object')
t.ok(get, 'Inventory returned getter')
Expand All @@ -78,7 +82,7 @@ testing
delete inv._project.globalPreferences._arc
delete inv._project.globalPreferences._raw
t.deepEqual(inv._project.globalPreferences, prefs, 'Got correct global preferences')
t.equal(inv._project.globalPreferencesFile, path, 'Got correct preferences file')
t.equal(inv._project.globalPreferencesFile, join(tmp, globalPrefsFile), 'Got correct preferences file')
t.teardown(reset)
}
})
Expand Down Expand Up @@ -132,6 +136,7 @@ test('Get local preferences', t => {

test('Layer local preferences over global preferences', t => {
t.plan(14)
let cwd = join(mock, 'prefs', 'local-over-global')
let globalPrefsText = `
@sandbox
environment testing
Expand All @@ -157,19 +162,6 @@ testing
production: null,
}
}
let localPrefsText = `
@sandbox
environment staging

@create
autocreate true

@env
testing
env_var_2 bar
staging
env_var_3 fiz
`
let localPrefs = {
sandbox: {
environment: 'staging',
Expand Down Expand Up @@ -198,16 +190,13 @@ staging
production: null,
}
}
let path = join(homedir(), '.prefs.arc')
mockFs({
'app.arc': arc,
[path]: globalPrefsText,
'preferences.arc': localPrefsText
let tmp = mockTmp({
[globalPrefsFile]: globalPrefsText,
})
inv({}, (err, result) => {
overrideHomedir(tmp)
inv({ cwd }, (err, result) => {
if (err) t.fail(err)
else {
mockFs.restore()
let { inv, get } = result
t.ok(inv, 'Inventory returned inventory object')
t.ok(get, 'Inventory returned getter')
Expand All @@ -226,8 +215,8 @@ staging
delete inv._project.localPreferences._raw
t.deepEqual(inv._project.globalPreferences, globalPrefs, 'Got correct global preferences')
t.deepEqual(inv._project.localPreferences, localPrefs, 'Got correct local preferences')
t.equal(inv._project.globalPreferencesFile, path, 'Got correct preferences file')
t.equal(inv._project.localPreferencesFile, join(process.cwd(), 'preferences.arc'), 'Got correct preferences file')
t.equal(inv._project.globalPreferencesFile, join(tmp, globalPrefsFile), 'Got correct preferences file')
t.equal(inv._project.localPreferencesFile, join(cwd, 'preferences.arc'), 'Got correct preferences file')
t.teardown(reset)
}
})
Expand All @@ -240,16 +229,15 @@ test('Preferences validation errors', async t => {
@env
foo
`
mockFs({
let cwd = mockTmp({
'app.arc': arc,
'prefs.arc': prefs,
})
try {
await inv({})
await inv({ cwd })
t.fail('Expected an error')
}
catch (err) {
mockFs.restore()
t.match(err.message, /Invalid preferences setting: @env foo/, 'Got back error message for invalid preferences')
}
})
18 changes: 17 additions & 1 deletion test/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
let os = require('os')
let { is } = require('../../src/lib')

let homedirBak
let tmpHomedir
function overrideHomedir (tmp) {
if (tmp) tmpHomedir = tmp
if (!homedirBak) homedirBak = os.homedir
os.homedir = () => tmpHomedir
}
overrideHomedir.reset = () => {
if (homedirBak) {
os.homedir = homedirBak
homedirBak = undefined
}
}

function setterPluginSetup (setter, fns) {
let methods = is.array(fns) ? fns : [ fns ]
methods = methods.map(m => {
Expand All @@ -11,5 +26,6 @@ function setterPluginSetup (setter, fns) {
}

module.exports = {
setterPluginSetup
overrideHomedir,
setterPluginSetup,
}
5 changes: 5 additions & 0 deletions test/mock/prefs/global/app.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@app
an-app

@events
an-event
5 changes: 5 additions & 0 deletions test/mock/prefs/local-over-global/app.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@app
an-app

@events
an-event
11 changes: 11 additions & 0 deletions test/mock/prefs/local-over-global/preferences.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@sandbox
environment staging

@create
autocreate true

@env
testing
env_var_2 bar
staging
env_var_3 fiz
5 changes: 5 additions & 0 deletions test/mock/prefs/local/app.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@app
an-app

@events
an-event
6 changes: 6 additions & 0 deletions test/mock/prefs/local/prefs.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@env
testing
foo bar

@create
autocreate true
9 changes: 4 additions & 5 deletions test/unit/src/config/arc-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let { join } = require('path')
let mockFs = require('mock-fs')
let mockTmp = require('mock-tmp')
let test = require('tape')
let sut = join(process.cwd(), 'src', 'config', 'arc')
let getArcConfig = require(sut)
Expand All @@ -11,12 +11,11 @@ test('Set up env', t => {

test('Set Arc version (if possible)', t => {
t.plan(1)
let cwd = process.cwd()
let path = join(cwd, 'node_modules', '@architect', 'architect', 'package.json')
let path = join('node_modules', '@architect', 'architect', 'package.json')
let version = 'lol'
let json = JSON.stringify({ version })
mockFs({ [path]: json })
let cwd = mockTmp({ [path]: json })
let arc = getArcConfig({ cwd, inventory: { _arc: {} } })
t.equal(arc.version, version, 'Got back installed arc version')
mockFs.restore()
mockTmp.reset()
})
Loading