Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
feat: do not alter file ownership (#96)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this module no longer attempts to change file ownership automatically
  • Loading branch information
nlf authored Oct 13, 2022
1 parent c62c19c commit 805535f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 43 deletions.
32 changes: 9 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@
const walkUp = require('walk-up-path')
const ini = require('ini')
const nopt = require('nopt')
const mkdirp = require('mkdirp-infer-owner')
const mapWorkspaces = require('@npmcli/map-workspaces')
const rpj = require('read-package-json-fast')
const log = require('proc-log')

/* istanbul ignore next */
const myUid = process.getuid && process.getuid()
/* istanbul ignore next */
const myGid = process.getgid && process.getgid()

const { resolve, dirname, join } = require('path')
const { homedir } = require('os')
const { promisify } = require('util')
const fs = require('fs')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const chmod = promisify(fs.chmod)
const chown = promisify(fs.chown)
const unlink = promisify(fs.unlink)
const stat = promisify(fs.stat)
const {
readFile,
writeFile,
chmod,
unlink,
stat,
mkdir,
} = require('fs/promises')

const hasOwnProperty = (obj, key) =>
Object.prototype.hasOwnProperty.call(obj, key)
Expand Down Expand Up @@ -731,16 +725,8 @@ class Config {
return
}
const dir = dirname(conf.source)
await mkdirp(dir)
await mkdir(dir, { recursive: true })
await writeFile(conf.source, iniData, 'utf8')
// don't leave a root-owned config file lying around
/* istanbul ignore if - this is best-effort and a pita to test */
if (myUid === 0) {
const st = await stat(dir).catch(() => null)
if (st && (st.uid !== myUid || st.gid !== myGid)) {
await chown(conf.source, st.uid, st.gid).catch(() => {})
}
}
const mode = where === 'user' ? 0o600 : 0o666
await chmod(conf.source, mode)
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"dependencies": {
"@npmcli/map-workspaces": "^2.0.2",
"ini": "^3.0.0",
"mkdirp-infer-owner": "^2.0.0",
"nopt": "^6.0.0",
"proc-log": "^2.0.0",
"read-package-json-fast": "^3.0.0",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const Umask = require('../../lib/type-defs.js').Umask.type
// used by cafile flattening to flatOptions.ca
const fs = require('fs')
const maybeReadFile = file => {
if (file.includes('WEIRD-ERROR')) {
throw Object.assign(new Error('weird error'), { code: 'EWEIRD' })
}

try {
return fs.readFileSync(file, 'utf8')
} catch (er) {
Expand Down
36 changes: 17 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
const t = require('tap')

// hackedy hacky hack
const fs = require('fs')
const { readFile, readFileSync } = fs
fs.readFile = (path, ...args) => {
if (path.match(/WEIRD-ERROR/)) {
const cb = args.pop()
cb(Object.assign(new Error('weird error'), { code: 'EWEIRD' }))
} else {
return readFile(path, ...args)
}
}
fs.readFileSync = (path, ...args) => {
if (path.match(/WEIRD-ERROR/)) {
throw Object.assign(new Error('weird error'), { code: 'EWEIRD' })
} else {
return readFileSync(path, ...args)
}
}
const { readFileSync } = fs

// when running with `npm test` it adds environment variables that
// mess with the things we expect here, so delete all of those.
Expand All @@ -34,9 +18,23 @@ const typeDefs = require('../lib/type-defs.js')

const { resolve, join, dirname } = require('path')

const Config = require('../')
const Config = t.mock('../', {
'fs/promises': {
...fs.promises,
readFile: async (path, ...args) => {
if (path.includes('WEIRD-ERROR')) {
throw Object.assign(new Error('weird error'), { code: 'EWEIRD' })
}

return fs.promises.readFile(path, ...args)
},
},
})

t.equal(typeDefs, Config.typeDefs, 'exposes type definitions')
// because we used t.mock above, the require cache gets blown and we lose our direct equality
// on the typeDefs. to get around that, we require an un-mocked Config and assert against that
const RealConfig = require('../')
t.equal(typeDefs, RealConfig.typeDefs, 'exposes type definitions')

t.test('construct with no settings, get default values for stuff', t => {
const npmPath = t.testdir()
Expand Down

0 comments on commit 805535f

Please sign in to comment.