Skip to content

Commit

Permalink
Update test coverage (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulshryock authored Jul 29, 2021
1 parent f0f8da9 commit 08c81b3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Update `file` util methods.
- Update test coverage.

### Deprecated

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"test": "ava",
"posttest": "rm -rf temp",
"test:watch": "ava --watch",
"test:coverage": "c8 npm test",
"test:coverage": "c8 npm test && c8 check-coverage",
"lint": "eslint . ./*.cjs src --fix",
"preversion": "npm run lint && npm test",
"preversion": "npm run lint && npm run test:coverage",
"version": "node cli.js -p -q && git add .",
"postversion": "git push && git push --tags && npm publish"
},
Expand Down
46 changes: 45 additions & 1 deletion src/Bump.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'ava'
import Bump from './Bump.js'
import { getType } from './utils/type.js'
import { mkdir, readFile, rm, writeFile } from 'fs/promises'

const defaults = {
paths: {
Expand All @@ -20,7 +21,7 @@ const badOptions = {
}

const temp = {
dir: 'temp',
dir: 'temp/bump',
}

const options = {
Expand All @@ -42,6 +43,21 @@ function hasDefaults (t, bump) {
t.is(bump.quiet, defaults.quiet, 'has the default quiet')
}

/**
* Write files.
*
* @since 2.2.0
*/
async function writeFiles () {
await mkdir(temp.dir, { recursive: true })

for (const path in defaults.paths) {
let data = await readFile(defaults.paths[path], 'utf8')
if (path === 'package') data = data.replace('repository', 'something-else')
await writeFile(options.paths[path], data)
}
}

/**
* Constructor tests.
*/
Expand Down Expand Up @@ -82,3 +98,31 @@ test('after setup', async t => {
t.is(getType(bump.changelog), 'object', 'changelog is an object.')
t.is(getType(bump.wordpress), 'object', 'wordpress is an object.')
})

/**
* Setup tests.
*/

test('after bump', async t => {
t.plan(4)

// Write files.
await writeFiles()

// Bump files.
const bump = new Bump(options)
await bump.init()

t.is(getType(bump.repository), 'string', 'repository is a string')
t.is(getType(bump.git), 'object', 'git is an object.')
t.is(getType(bump.changelog), 'object', 'changelog is an object.')
t.is(getType(bump.wordpress), 'object', 'wordpress is an object.')
})

/**
* Cleanup
*/

test.after('cleanup', async () => {
await rm(temp.dir, { recursive: true, force: true })
})
12 changes: 6 additions & 6 deletions src/Git.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default class Git {
try {
// Handle async setup tasks.
await this.setup()
} catch (p) {
console.error(chalk.red(p.stderr))
$`exit 1`
} catch (error) {
console.error(chalk.red(error))
throw error
}
}

Expand Down Expand Up @@ -49,9 +49,9 @@ export default class Git {
}
this.tags.current = this.tags.all
.filter(tag => tag.includes(this.version))
} catch (p) {
console.error(chalk.red(p.stderr))
await $`exit 1`
} catch (error) {
console.error(chalk.red(error))
throw error
}
}
}
24 changes: 5 additions & 19 deletions src/WordPress.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { $, chalk } from 'zx'
import { chalk } from 'zx'
import { writeFile } from 'fs/promises'
import { parse } from 'path'
import { getFileContent } from './utils/file.js'
import { getType } from './utils/type.js'

$.verbose = false

/**
* WordPress class.
*
Expand All @@ -31,10 +29,7 @@ export default class WordPress {
? quiet
: false)
this.version = getType(version) === 'string' ? version : null
} catch (error) {
console.error(chalk.red(error))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand All @@ -52,10 +47,7 @@ export default class WordPress {

// Bump.
await this.bump()
} catch (p) {
console.error(p.stderr)
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand Down Expand Up @@ -86,10 +78,7 @@ export default class WordPress {
delete this.plugin
delete this.theme
}
} catch (error) {
console.error(chalk.red(error))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand Down Expand Up @@ -148,9 +137,6 @@ export default class WordPress {
writeFile(this.plugin.path, this.plugin.new, 'utf8')
if (!this.quiet)console.log(chalk.green('Bumped WordPress plugin.'))
}
} catch (error) {
console.error(chalk.red(error))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}
}
15 changes: 3 additions & 12 deletions src/bump.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export default class Bump {

// Bump.
await this.bump()
} catch (p) {
console.error(chalk.red(p.stderr))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand Down Expand Up @@ -99,10 +96,7 @@ export default class Bump {

// Initialize WordPress.
this.wordpress = new WordPress({ quiet: this.quiet, version })
} catch (p) {
console.error(chalk.red(p.stderr))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand All @@ -117,9 +111,6 @@ export default class Bump {

// Bump WordPress.
await this.wordpress.init()
} catch (p) {
console.error(chalk.red(p.stderr))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}
}
40 changes: 15 additions & 25 deletions src/changelog.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { $, chalk } from 'zx'
import { chalk } from 'zx'
import { mkdir, writeFile } from 'fs/promises'
import { parse } from 'path'
import { getFileContent } from './utils/file.js'
import { getType } from './utils/type.js'

$.verbose = false

/**
* Changelog class.
*
Expand Down Expand Up @@ -49,17 +47,21 @@ export default class Changelog {
}

this.path = path && getType(path) === 'string' ? path : defaults.path
this.prefix = prefix && getType(prefix) === 'string' ? prefix : defaults.prefix
this.prefix = prefix && getType(prefix) === 'string'
? prefix
: defaults.prefix
this.quiet = process.env.NODE_ENV === 'test'
? true
: (getType(quiet) === 'boolean'
? quiet
: defaults.quiet)
this.remote = remote && getType(remote) === 'string' ? remote : defaults.remote
: (getType(quiet) === 'boolean' ? quiet : defaults.quiet)
this.remote = remote && getType(remote) === 'string'
? remote
: defaults.remote
this.repository = repository && getType(repository) === 'string'
? repository
: defaults.repository
this.version = version && getType(version) === 'string' ? version : defaults.version
this.version = version && getType(version) === 'string'
? version
: defaults.version
this.#header = `## [${this.version}](${this.repository}/` +
`${this.remote === 'bitbucket' ? 'commits/tag' : 'releases/tag'}/` +
`${this.prefix}${this.version}) - ${this.#today}`
Expand All @@ -73,10 +75,7 @@ export default class Changelog {
'\n\n### Removed' +
'\n\n### Fixed' +
'\n\n### Security'
} catch (error) {
console.error(chalk.red(error))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand All @@ -91,10 +90,7 @@ export default class Changelog {

// Bump.
await this.bump()
} catch (error) {
console.error(chalk.red(error))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand Down Expand Up @@ -144,10 +140,7 @@ export default class Changelog {
this.#header,
this.#unreleased + '\n\n' + this.#header,
)
} catch (error) {
console.error(chalk.red(error))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}

/**
Expand All @@ -161,9 +154,6 @@ export default class Changelog {
if (directory) await mkdir(directory, { recursive: true })
await writeFile(this.path, this.new, 'utf8')
if (!this.quiet) console.log(chalk.green('Bumped Changelog.'))
} catch (error) {
console.error(chalk.red(error))
$`exit 1`
}
} catch (error) { console.error(chalk.red(error)) }
}
}

0 comments on commit 08c81b3

Please sign in to comment.