Skip to content

Commit

Permalink
feat: add github-workflow outdated script
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Mar 25, 2021
1 parent d923463 commit f8317b9
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
29 changes: 29 additions & 0 deletions bin/commands/github-workflow/outdated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const fs = require('fs')
const path = require('path')

exports.desc = 'Check if you have the bundled version of wiby Github workflow installed.'

exports.handler = async (params) => {
const packageRoot = process.env.INIT_CWD || process.cwd()

const workflowsPath = path.join(packageRoot, '.github', 'workflows')
const sourceWibyYaml = path.join(__dirname, '..', '..', '..', '.github', 'workflows', 'wiby.yaml')
const destWibyYaml = path.join(workflowsPath, 'wiby.yaml')

if (!fs.existsSync(destWibyYaml)) {
console.error(`${destWibyYaml} not found. Use \`wiby github-workflow install\` to install it.`)
process.exit(1)
}

const expectedContents = fs.readFileSync(sourceWibyYaml)
const actualContents = fs.readFileSync(destWibyYaml)

if (Buffer.compare(expectedContents, actualContents) !== 0) {
console.error(`${destWibyYaml} is not the same as the bundled version at ${sourceWibyYaml}. Use \`wiby github-workflow install\` to install it.`)
process.exit(1)
}

console.log(`${destWibyYaml} is the same as the bundled version.`)
}
8 changes: 4 additions & 4 deletions test/cli/github-workflow-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ tap.test('github-workflow install command', async (tap) => {
...process.env,
INIT_CWD: ''
}
}).toString()
})

tap.notEqual(fs.readFileSync(wibyYamlPath).toString(), contentsBefore)
})

tap.test('should copy wiby.yaml to the ${INIT_CWD}/.github/workflows folder', async (tap) => {
tap.test('should copy wiby.yaml to the $INIT_CWD/.github/workflows folder', async (tap) => {
const initCwd = path.join(process.cwd(), 'some-other-place')
const workflowsPath = path.join(initCwd, '.github', 'workflows')
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
Expand All @@ -46,7 +46,7 @@ tap.test('github-workflow install command', async (tap) => {
...process.env,
INIT_CWD: initCwd
}
}).toString()
})

tap.notEqual(fs.readFileSync(wibyYamlPath).toString(), contentsBefore)
})
Expand All @@ -58,7 +58,7 @@ tap.test('github-workflow install command', async (tap) => {
...process.env,
INIT_CWD: ''
}
}).toString()
})
tap.fail('Should fail before reaching here')
} catch (err) {
tap.include(err.message, '/.github/workflows folder does not exist.')
Expand Down
88 changes: 88 additions & 0 deletions test/cli/github-workflow-outdated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict'

const childProcess = require('child_process')
const fs = require('fs')
const path = require('path')
const tap = require('tap')

const gitFixture = require('../fixtures/git')

const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')

tap.test('github-workflow outdated command', async (tap) => {
tap.beforeEach(async () => {
gitFixture.init()
})

tap.test('should fail when wiby.yaml is missing', async (tap) => {
try {
childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
env: {
...process.env,
INIT_CWD: ''
}
})
tap.fail('Should fail before reaching here')
} catch (err) {
tap.include(err.message, '/.github/workflows/wiby.yaml not found. Use `wiby github-workflow install` to install it.')
}
})

tap.test('should fail when wiby.yaml has the wrong contents', async (tap) => {
const workflowsPath = path.join(process.cwd(), '.github', 'workflows')
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')
const contentsBefore = 'should be overwritten with new version'

fs.mkdirSync(workflowsPath, { recursive: true })
fs.writeFileSync(wibyYamlPath, contentsBefore)

try {
childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
env: {
...process.env,
INIT_CWD: ''
}
})
tap.fail('Should fail before reaching here')
} catch (err) {
tap.include(err.message, '/.github/workflows/wiby.yaml is not the same as the bundled version')
}
})

tap.test('should pass when wiby.yaml has the same contents', async (tap) => {
const originalContents = fs.readFileSync(path.join(__dirname, '..', '..', '.github', 'workflows', 'wiby.yaml'))
const workflowsPath = path.join(process.cwd(), '.github', 'workflows')
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')

fs.mkdirSync(workflowsPath, { recursive: true })
fs.writeFileSync(wibyYamlPath, originalContents)

const result = childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
env: {
...process.env,
INIT_CWD: ''
}
}).toString()

tap.include(result, 'wiby.yaml is the same as the bundled version.')
})

tap.test('should pass when wiby.yaml has the same contents in $INIT_CWD', async (tap) => {
const originalContents = fs.readFileSync(path.join(__dirname, '..', '..', '.github', 'workflows', 'wiby.yaml'))
const initCwd = path.join(process.cwd(), 'some-other-place')
const workflowsPath = path.join(initCwd, '.github', 'workflows')
const wibyYamlPath = path.join(workflowsPath, 'wiby.yaml')

fs.mkdirSync(workflowsPath, { recursive: true })
fs.writeFileSync(wibyYamlPath, originalContents)

const result = childProcess.execSync(`${wibyCommand} github-workflow outdated`, {
env: {
...process.env,
INIT_CWD: initCwd
}
}).toString()

tap.include(result, 'wiby.yaml is the same as the bundled version.')
})
})
2 changes: 1 addition & 1 deletion test/cli/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tap.test('result command', async (tap) => {

tap.test('result command should fail when config and dependent provided', async (tap) => {
try {
childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`).toString()
childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`)
tap.fail()
} catch (err) {
tap.equal(true, err.message.includes('Arguments dependent and config are mutually exclusive'))
Expand Down

0 comments on commit f8317b9

Please sign in to comment.