Skip to content

Commit

Permalink
test: replace setup/teardown with functional approach (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Jun 16, 2018
1 parent 9fb125a commit f41e884
Showing 1 changed file with 56 additions and 47 deletions.
103 changes: 56 additions & 47 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,94 +7,103 @@ import pkg from '../package.json'

const readFile = file => fs.readFile(file, 'utf8')

suite('init', () => {
let workDir
let originalDir

const fixturePath = name => path.join(__dirname, 'fixtures', name)

const fixture = async name => {
const src = fixturePath(name)
const dest = path.join(workDir, 'package.json')
await fs.copy(src, dest)
return dest
const sandbox = async fn => {
const workDir = path.join(os.tmpdir(), `${pkg.name}${Date.now()}`)
await fs.mkdirs(workDir)
const origDir = process.cwd()
process.chdir(workDir)

try {
const fixturePath = name => path.join(__dirname, 'fixtures', name)
const fixture = async name => {
const src = fixturePath(name)
const dest = path.join(workDir, 'package.json')
await fs.copy(src, dest)
return dest
}
const readFixture = name => readFile(fixturePath(name))
const readOrigFile = name => readFile(path.join(origDir, name))
const readWorkFile = name => readFile(path.join(workDir, name))

return await fn({
fixturePath,
fixture,
readFixture,
readOrigFile,
readWorkFile,
})
} finally {
process.chdir(origDir)
await fs.remove(workDir)
}
}

setup('work directory', async () => {
workDir = path.join(os.tmpdir(), `${pkg.name}${Date.now()}`)
await fs.mkdirs(workDir)

originalDir = process.cwd()
process.chdir(workDir)
})
const testInSandbox = (name, fn) => {
test(name, () => sandbox(fn))
}

teardown(async () => {
process.chdir(originalDir)

await fs.remove(workDir)
})

test('update "package.json"', async () => {
const src = await fixture('package-normal.json')
suite('init', () => {
testInSandbox('update "package.json"', async ctx => {
const src = await ctx.fixture('package-normal.json')
await exec('init')
const actual = await readFile(src)
const expected = await readFile(fixturePath('package-normal_expected.json'))
const expected = await ctx.readFixture('package-normal_expected.json')
assert(actual === expected)
})

test('update "package.json" without fields', async () => {
const src = await fixture('package-empty.json')
testInSandbox('update "package.json" without fields', async ctx => {
const src = await ctx.fixture('package-empty.json')
await exec('init')
const actual = await readFile(src)
const expected = await readFile(fixturePath('package-empty_expected.json'))
const expected = await ctx.readFixture('package-empty_expected.json')
assert(actual === expected)
})

test('write ".editorconfig"', async () => {
await fixture('package-normal.json')
testInSandbox('write ".editorconfig"', async ctx => {
await ctx.fixture('package-normal.json')
const { stdout, stderr } = await exec('init')
assert(stdout.includes('package.json was updated.'))
assert(stderr === '')

const original = await readFile(path.join(originalDir, '.editorconfig'))
const copy = await readFile(path.join(workDir, '.editorconfig'))
const original = await ctx.readOrigFile('.editorconfig')
const copy = await ctx.readWorkFile('.editorconfig')
assert(original === copy)
})

test('write ".prettierignore"', async () => {
await fixture('package-normal.json')
testInSandbox('write ".prettierignore"', async ctx => {
await ctx.fixture('package-normal.json')
const { stdout, stderr } = await exec('init')
assert(stdout.includes('package.json was updated.'))
assert(stderr === '')

const original = await readFile(path.join(originalDir, '.prettierignore'))
const copy = await readFile(path.join(workDir, '.prettierignore'))
const original = await ctx.readOrigFile('.prettierignore')
const copy = await ctx.readWorkFile('.prettierignore')
assert(original === copy)
})

test('write ".eslintrc.js"', async () => {
await fixture('package-normal.json')
testInSandbox('write ".eslintrc.js"', async ctx => {
await ctx.fixture('package-normal.json')
const { stdout, stderr } = await exec('init')
assert(stdout.includes('.eslintrc.js was updated.'))
assert(stderr === '')

const actual = await readFile(path.join(workDir, '.eslintrc.js'))
const expected = await readFile(fixturePath('.eslintrc_expected.js'))
const actual = await ctx.readWorkFile('.eslintrc.js')
const expected = await ctx.readFixture('.eslintrc_expected.js')
assert(actual === expected)
})

test('write ".commitlintrc.js"', async () => {
await fixture('package-normal.json')
testInSandbox('write ".commitlintrc.js"', async ctx => {
await ctx.fixture('package-normal.json')
const { stdout, stderr } = await exec('init')
assert(stdout.includes('.commitlintrc.js was updated.'))
assert(stderr === '')

const actual = await readFile(path.join(workDir, '.commitlintrc.js'))
const expected = await readFile(fixturePath('.commitlintrc_expected.js'))
const actual = await ctx.readWorkFile('.commitlintrc.js')
const expected = await ctx.readFixture('.commitlintrc_expected.js')
assert(actual === expected)
})

test('throw error if no package.json', async () => {
testInSandbox('throw error if no package.json', async () => {
const error = await exec('init').catch(err => err)
assert(error instanceof Error)
assert(error.code === 1)
Expand Down

0 comments on commit f41e884

Please sign in to comment.