Skip to content

Commit

Permalink
test: move test tools to TS (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Nov 27, 2023
1 parent 27f3642 commit 8052c60
Show file tree
Hide file tree
Showing 25 changed files with 232 additions and 21 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
{
"files": [
"tools/**/*.ts",
"packages/**/*.ts"
],
"extends": [
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-angular/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import { describe, it, expect } from 'vitest'
import BetterThanBefore from 'better-than-before'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

const { setups, preparing, tearsWithJoy } = BetterThanBefore()
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-atom/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-cli/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import path from 'path'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'

const CLI_PATH = path.join(__dirname, '../cli.js')
const FIXTURE_CHANGELOG_PATH = path.join(__dirname, 'fixtures/_CHANGELOG.md')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

let testTools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import { describe, it, expect } from 'vitest'
import BetterThanBefore from 'better-than-before'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset, { DEFAULT_COMMIT_TYPES } from '../index.js'

const { setups, preparing, tearsWithJoy } = BetterThanBefore()
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-core/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest'
import BetterThanBefore from 'better-than-before'
import path from 'path'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import conventionalChangelogCore from '../index.js'

const { setups, preparing, tearsWithJoy } = BetterThanBefore()
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-ember/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeEach, afterEach, it, expect } from 'vitest'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-eslint/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-express/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeEach, afterEach, it, expect } from 'vitest'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-jquery/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeEach, afterEach, it, expect } from 'vitest'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-jshint/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import conventionalChangelogCore from 'conventional-changelog-core'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import preset from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-writer/test/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'
import { describe, beforeAll, it, expect } from 'vitest'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'

const CLI_PATH = path.join(__dirname, './test-cli.cjs')
const COMMITS_PATH = 'fixtures/commits.ldjson'
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-writer/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dedent from 'dedent'
import { describe, it, expect } from 'vitest'
import { delay, throughObj } from '../../../tools/test-tools.js'
import { delay, throughObj } from '../../../tools/test-tools.ts'
import conventionalChangelogWriter, { parseArray } from '../index.js'

function formatDate (date, timeZone = 'UTC') {
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog-writer/test/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { delay } from '../../../tools/test-tools.js'
import { delay } from '../../../tools/test-tools.ts'
import {
compileTemplates,
functionify,
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-changelog/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import conventionalChangelog from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/conventional-recommended-bump/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest'
import BetterThanBefore from 'better-than-before'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import conventionalRecommendedBump from '../index.js'

const { setups, preparing, tearsWithJoy } = BetterThanBefore()
Expand Down
2 changes: 1 addition & 1 deletion packages/git-raw-commits/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import { TestTools, delay } from '../../../tools/test-tools.js'
import { TestTools, delay } from '../../../tools/test-tools.ts'
import gitRawCommits from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/git-semver-tags/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import gitSemverTags from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/gulp-conventional-changelog/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Buffer } from 'safe-buffer'
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import { join } from 'path'
import Vinyl from 'vinyl'
import { TestTools, through } from '../../../tools/test-tools.js'
import { TestTools, through } from '../../../tools/test-tools.ts'
import conventionalChangelog from '../index.js'

let testTools
Expand Down
2 changes: 1 addition & 1 deletion packages/standard-changelog/test/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { describe, beforeAll, beforeEach, afterAll, it, expect } from 'vitest'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'

const CLI_PATH = path.join(__dirname, '../cli.js')
const FIXTURE_CHANGELOG_PATH = path.join(__dirname, 'fixtures/_CHANGELOG.md')
Expand Down
2 changes: 1 addition & 1 deletion packages/standard-changelog/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, beforeAll, afterAll, it, expect } from 'vitest'
import { TestTools } from '../../../tools/test-tools.js'
import { TestTools } from '../../../tools/test-tools.ts'
import standardChangelog from '../index.js'

let testTools
Expand Down
13 changes: 13 additions & 0 deletions tools/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": [
"@trigen/eslint-config/typescript",
"@trigen/eslint-config/typescript-requiring-type-checking"
],
"parserOptions": {
"tsconfigRootDir": "./tools",
"project": ["./tsconfig.json"]
},
"rules": {
"@typescript-eslint/no-magic-numbers": "off"
}
}
191 changes: 191 additions & 0 deletions tools/test-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import { execSync, spawn } from 'child_process'
import { Transform } from 'stream'
import { pathToFileURL } from 'url'
import path from 'path'
import fs from 'fs'
// @ts-expect-error 'tmp' has no types
import tmp from 'tmp'

export function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}

function fixMessage(message?: string) {
let msg = message

if (!msg || typeof msg !== 'string') {
msg = 'Test commit'
}

// we need to escape backtick for bash but not for windows
// probably this should be done in git-dummy-commit or shelljs
if (process.platform !== 'win32') {
msg = msg.replace(/`/g, '\\`')
}

return `"${msg}"`
}

function prepareMessageArgs(msg: string | string[]) {
const args = []

if (Array.isArray(msg)) {
if (msg.length > 0) {
for (const m of msg) {
args.push('-m', fixMessage(m))
}
} else {
args.push('-m', fixMessage())
}
} else {
args.push('-m', fixMessage(msg))
}

return args
}

export function through(
transform = (
chunk: string | Buffer,
_enc: string,
cb: (err: Error | null, chunk: string | Buffer) => void
) => cb(null, chunk),
flush?: () => void
) {
return new Transform({
transform,
flush
})
}

export function throughObj(
transform = (
chunk: string | Buffer,
_enc: string,
cb: (err: Error | null, chunk: string | Buffer) => void
) => cb(null, chunk),
flush?: () => void
) {
return new Transform({
objectMode: true,
highWaterMark: 16,
transform,
flush
})
}

export class TestTools {
cwd: string

constructor(cwd: string) {
this.cwd = cwd

if (!cwd) {
/* eslint-disable */
this.cwd = fs.realpathSync(tmp.dirSync().name)
tmp.setGracefulCleanup()
/* eslint-enable */
}
}

cleanup() {
try {
this.rmSync(this.cwd, {
recursive: true
})
} catch (err) {
// ignore
}
}

mkdirSync(dir: string, options?: Parameters<typeof fs.mkdirSync>[1]) {
return fs.mkdirSync(path.resolve(this.cwd, dir), options)
}

writeFileSync(file: string, content: string) {
return fs.writeFileSync(path.resolve(this.cwd, file), content)
}

readFileSync(file: string, options: Parameters<typeof fs.readFileSync>[1]) {
return fs.readFileSync(path.resolve(this.cwd, file), options)
}

rmSync(target: string, options?: Parameters<typeof fs.rmSync>[1]) {
return fs.rmSync(path.resolve(this.cwd, target), options)
}

exec(command: string) {
return execSync(command, {
cwd: this.cwd,
stdio: 'pipe',
encoding: 'utf-8'
})
}

fork(script: string, args: string[] = [], options: Parameters<typeof spawn>[2] = {}) {
return new Promise<{stdout: string, stderr: string, exitCode: number | null}>((resolve, reject) => {
const finalOptions = {
cwd: this.cwd,
stdio: [
null,
null,
null
],
...options
}
const nodeArgs = [
'--no-warnings',
'--loader',
pathToFileURL(path.resolve(__dirname, '..', 'node_modules', 'tsm', 'loader.mjs')).toString()
]
const child = spawn(process.execPath, [
...nodeArgs,
script,
...args
], finalOptions)
let stdout = ''
let stderr = ''
let exitCode = null

child.stdout?.on('data', (data: Buffer) => {
stdout += data.toString()
})
child.stderr?.on('data', (data: Buffer) => {
stderr += data.toString()
})
child.on('close', (code) => {
exitCode = code
resolve({
stdout,
stderr,
exitCode
})
})
child.on('error', reject)
})
}

gitInit() {
this.mkdirSync('git-templates')
return this.exec('git init --template=./git-templates --initial-branch=master')
}

gitDummyCommit(msg: string | string[]) {
const args = prepareMessageArgs(msg)

args.push(
'--allow-empty',
'--no-gpg-sign'
)

return this.exec(`git commit ${args.join(' ')}`)
}

gitTails() {
const data = execSync('git rev-list --parents HEAD', {
cwd: this.cwd
})

return data.toString().match(/^[a-f0-9]{40}$/gm)
}
}
6 changes: 6 additions & 0 deletions tools/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"include": [
"."
]
}

0 comments on commit 8052c60

Please sign in to comment.