Skip to content

Commit

Permalink
Override os.homedir() instead of leaking testing concerns into busi…
Browse files Browse the repository at this point in the history
…ness logic
  • Loading branch information
ryanblock committed Dec 7, 2023
1 parent 3f1b7f1 commit 22c4c47
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 97 deletions.
4 changes: 2 additions & 2 deletions src/config/project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let { is, mergeEnvVars } = require('../../lib')
* Get the project-level configuration, overlaying arc.aws settings (if present)
*/
module.exports = function getProjectConfig (params) {
let { arc, errors, raw, filepath, inventory, _testing } = params
let { arc, errors, raw, filepath, inventory } = params
let _project = {
...inventory._project,
arc,
Expand All @@ -26,7 +26,7 @@ module.exports = function getProjectConfig (params) {
// Parse local and global project preferences
let scopes = [ 'global', 'local' ]
for (let scope of scopes) {
let p = prefs({ scope, inventory, errors, _testing })
let p = prefs({ scope, inventory, errors })
if (p) {
// Set up the scoped metadata
_project[`${scope}Preferences`] = p.preferences
Expand Down
13 changes: 3 additions & 10 deletions src/config/project/prefs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ 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, _testing }) {
module.exports = function getPrefs ({ scope, inventory, errors }) {
let cwd = scope === 'global'
? homedir()
? os.homedir()
: inventory._project.cwd

/* istanbul ignore next */
if (_testing && scope === 'global') {
let _homedir = homedir()
if (process.platform === 'win32') _homedir = _homedir.replace(/^[A-Z]:\\/, '')
cwd = join(inventory._project.cwd, _homedir)
}

let envFilepath = join(cwd, '.env')
let hasEnvFile = scope === 'local' && existsSync(envFilepath)
let prefs = read({ type: 'preferences', cwd, errors })
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function architectInventory (params = {}, callback) {

// Always ensure we have a working dir
params.cwd = params.cwd || process.cwd()
let { cwd, rawArc, _testing } = params
let { cwd, rawArc } = params

// Stateless inventory run
if (rawArc) {
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = function architectInventory (params = {}, callback) {
let inventory = inventoryDefaults(params)

// Set up project params for config
let project = { arc, cwd, errors, filepath, inventory, raw, _testing }
let project = { arc, cwd, errors, filepath, inventory, raw }

// Populate inventory.arc
inventory._arc = config._arc(project)
Expand Down
49 changes: 18 additions & 31 deletions test/integration/preferences-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ let test = require('tape')
let mockTmp = require('mock-tmp')
let cwd = process.cwd()
let testLibPath = join(cwd, 'test', 'lib')
let { getHomedir } = require(testLibPath)
let { overrideHomedir } = require(testLibPath)
let sut = join(cwd, 'src', 'index')
let inv = require(sut)

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

/**
* Duplicates some unit tests as part of the larger whole integration test
Expand All @@ -24,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 @@ -59,12 +61,11 @@ testing
env_var_1 foo
env_var_2 bar
`
let path = join(_homedir, '.prefs.arc')
let cwd = mockTmp({
'app.arc': arc,
[path]: prefsText
let tmp = mockTmp({
[globalPrefsFile]: prefsText
})
inv({ cwd, _testing }, (err, result) => {
overrideHomedir(tmp)
inv({ cwd }, (err, result) => {
if (err) t.fail(err)
else {
let { inv, get } = result
Expand All @@ -81,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, join(cwd, path), 'Got correct preferences file')
t.equal(inv._project.globalPreferencesFile, join(tmp, globalPrefsFile), 'Got correct preferences file')
t.teardown(reset)
}
})
Expand Down Expand Up @@ -135,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 @@ -160,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 @@ -201,13 +190,11 @@ staging
production: null,
}
}
let path = join(_homedir, '.prefs.arc')
let cwd = mockTmp({
'app.arc': arc,
[path]: globalPrefsText,
'preferences.arc': localPrefsText
let tmp = mockTmp({
[globalPrefsFile]: globalPrefsText,
})
inv({ cwd, _testing }, (err, result) => {
overrideHomedir(tmp)
inv({ cwd }, (err, result) => {
if (err) t.fail(err)
else {
let { inv, get } = result
Expand All @@ -228,7 +215,7 @@ 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, join(cwd, path), '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 Down
20 changes: 14 additions & 6 deletions test/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
let { homedir } = require('os')
let os = require('os')
let { is } = require('../../src/lib')

function getHomedir () {
let _homedir = homedir()
if (process.platform === 'win32') _homedir = _homedir.replace(/^[A-Z]:\\/, '')
return _homedir
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) {
Expand All @@ -18,6 +26,6 @@ function setterPluginSetup (setter, fns) {
}

module.exports = {
getHomedir,
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
37 changes: 15 additions & 22 deletions test/unit/src/config/project/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ let test = require('tape')
let mockTmp = require('mock-tmp')
let cwd = process.cwd()
let testLibPath = join(cwd, 'test', 'lib')
let { getHomedir } = require(testLibPath)
let { overrideHomedir } = require(testLibPath)
let inventoryDefaultsPath = join(process.cwd(), 'src', 'defaults')
let inventoryDefaults = require(inventoryDefaultsPath)
let defaultFunctionConfigPath = join(process.cwd(), 'src', 'defaults', 'function-config')
let defaultFunctionConfig = require(defaultFunctionConfigPath)
let sut = join(cwd, 'src', 'config', 'project')
let getProjectConfig = require(sut)

let mock = join(cwd, 'test', 'mock')
let localPrefsFile = 'prefs.arc'
let _homedir = getHomedir()
let globalPrefsFile = join(_homedir, 'prefs.arc')
let _testing = true
let globalPrefsFile = '.prefs.arc'

test('Set up env', t => {
t.plan(1)
Expand Down Expand Up @@ -67,17 +66,10 @@ test('Project preferences', t => {
t.plan(29)
let arc = {}
let errors = []
let cwd, inventory, proj
let cwd, inventory, proj, tmp

// Local preferences only
let localPrefs = `@env
testing
foo bar
@create
autocreate true`
cwd = mockTmp({
[localPrefsFile]: localPrefs
})
cwd = join(mock, 'prefs', 'local')
inventory = inventoryDefaults({ cwd })
proj = getProjectConfig({ arc, errors, inventory })
t.equal(errors.length, 0, 'Did not error')
Expand All @@ -90,37 +82,37 @@ autocreate true`
t.equal(proj.globalPreferences, null, 'Did not populate globalPreferences')
t.equal(proj.globalPreferencesFile, null, 'Did not populate globalPreferencesFile')
t.equal(proj.env.local.testing.foo, 'bar', 'Populated env local/testing')
mockTmp.reset()

// Global preferences only
let globalPrefs = `@env
testing
fiz buz
@sandbox
useAWS true`
cwd = mockTmp({
tmp = mockTmp({
[globalPrefsFile]: globalPrefs
})
inventory = inventoryDefaults({ cwd })
proj = getProjectConfig({ arc, errors, inventory, _testing })
overrideHomedir(tmp)
inventory = inventoryDefaults({ cwd: process.cwd() })
proj = getProjectConfig({ arc, errors, inventory })
t.equal(errors.length, 0, 'Did not error')
t.ok(proj.preferences, 'Populated preferences')
t.equal(proj.preferences.env.testing.fiz, 'buz', 'Populated testing env')
t.equal(proj.preferences.sandbox.useAWS, true, 'Populated Sandbox prefs')
t.equal(proj.localPreferences, null, 'Did not populate localPreferences')
t.equal(proj.localPreferencesFile, null, 'Did not populate localPreferencesFile')
t.ok(proj.globalPreferences, 'Populated globalPreferences')
t.equal(proj.globalPreferencesFile, join(cwd, globalPrefsFile), 'Populated globalPreferencesFile')
t.equal(proj.globalPreferencesFile, join(tmp, globalPrefsFile), 'Populated globalPreferencesFile')
t.equal(proj.env.local.testing.fiz, 'buz', 'Populated env local/testing')
mockTmp.reset()

// Merge local + global preferences
cwd = mockTmp({
[localPrefsFile]: localPrefs,
tmp = mockTmp({
[globalPrefsFile]: globalPrefs,
})
overrideHomedir(tmp)
inventory = inventoryDefaults({ cwd })
proj = getProjectConfig({ arc, errors, inventory, _testing })
proj = getProjectConfig({ arc, errors, inventory })
t.equal(errors.length, 0, 'Did not error')
t.ok(proj.preferences, 'Populated preferences')
t.equal(proj.preferences.env.testing.foo, 'bar', 'Populated testing env (preferred local to global prefs)')
Expand All @@ -129,9 +121,10 @@ useAWS true`
t.ok(proj.localPreferences, 'Populated localPreferences')
t.equal(proj.localPreferencesFile, join(cwd, localPrefsFile), 'Populated localPreferencesFile')
t.ok(proj.globalPreferences, 'Populated globalPreferences')
t.equal(proj.globalPreferencesFile, join(cwd, globalPrefsFile), 'Populated globalPreferencesFile')
t.equal(proj.globalPreferencesFile, join(tmp, globalPrefsFile), 'Populated globalPreferencesFile')
t.equal(proj.env.local.testing.foo, 'bar', 'Populated env local/testing (preferred local to global prefs)')
mockTmp.reset()
overrideHomedir.reset()
})

test('Project plugins', t => {
Expand Down
Loading

0 comments on commit 22c4c47

Please sign in to comment.