Skip to content

Commit

Permalink
feat: first projects
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 27, 2023
1 parent 84a30c3 commit d3f4fdd
Show file tree
Hide file tree
Showing 9 changed files with 1,303 additions and 21 deletions.
Empty file added builds/.gitkeep
Empty file.
177 changes: 177 additions & 0 deletions ecosystem-ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import fs from 'fs'
import path from 'path'
import process from 'process'
import { cac } from 'cac'

import {
setupEnvironment,
setupVitestRepo,
buildVitest,
bisectVitest,
parseVitestMajor,
parseMajorVersion,
} from './utils'
import { CommandOptions, RunOptions } from './types'

const cli = cac()
cli
.command('[...suites]', 'build vitest and run selected suites')
.option('--verify', 'verify checkouts by running tests', { default: false })
.option('--repo <repo>', 'vitest repository to use', {
default: 'vitest-dev/vitest',
})
.option('--branch <branch>', 'vitest branch to use', { default: 'main' })
.option('--tag <tag>', 'vitest tag to use')
.option('--commit <commit>', 'vitest commit sha to use')
.option('--release <version>', 'vitest release to use from npm registry')
.action(async (suites, options: CommandOptions) => {
const { root, vitestPath, workspace } = await setupEnvironment()
const suitesToRun = getSuitesToRun(suites, root)
let vitestMajor
if (!options.release) {
await setupVitestRepo(options)
await buildVitest({ verify: options.verify })
vitestMajor = parseVitestMajor(vitestPath)
} else {
vitestMajor = parseMajorVersion(options.release)
}
const runOptions: RunOptions = {
root,
vitestPath,
vitestMajor,
workspace,
release: options.release,
verify: options.verify,
skipGit: false,
}
for (const suite of suitesToRun) {
await run(suite, runOptions)
}
})

cli
.command('build-vitest', 'build vitest only')
.option('--verify', 'verify vitest checkout by running tests', {
default: false,
})
.option('--repo <repo>', 'vitest repository to use', {
default: 'vitest-dev/vitest',
})
.option('--branch <branch>', 'vitest branch to use', { default: 'main' })
.option('--tag <tag>', 'vitest tag to use')
.option('--commit <commit>', 'vitest commit sha to use')
.action(async (options: CommandOptions) => {
await setupEnvironment()
await setupVitestRepo(options)
await buildVitest({ verify: options.verify })
})

cli
.command('run-suites [...suites]', 'run single suite with pre-built vitest')
.option(
'--verify',
'verify checkout by running tests before using local vitest',
{ default: false },
)
.option('--repo <repo>', 'vitest repository to use', {
default: 'vitest-dev/vitest',
})
.option('--release <version>', 'vitest release to use from npm registry')
.action(async (suites, options: CommandOptions) => {
const { root, vitestPath, workspace } = await setupEnvironment()
const suitesToRun = getSuitesToRun(suites, root)
const runOptions: RunOptions = {
...options,
root,
vitestPath,
vitestMajor: parseVitestMajor(vitestPath),
workspace,
}
for (const suite of suitesToRun) {
await run(suite, runOptions)
}
})

cli
.command(
'bisect [...suites]',
'use git bisect to find a commit in vitest that broke suites',
)
.option('--good <ref>', 'last known good ref, e.g. a previous tag. REQUIRED!')
.option('--verify', 'verify checkouts by running tests', { default: false })
.option('--repo <repo>', 'vitest repository to use', {
default: 'vitest-dev/vitest',
})
.option('--branch <branch>', 'vitest branch to use', { default: 'main' })
.option('--tag <tag>', 'vitest tag to use')
.option('--commit <commit>', 'vitest commit sha to use')
.action(async (suites, options: CommandOptions & { good: string }) => {
if (!options.good) {
console.log(
'you have to specify a known good version with `--good <commit|tag>`',
)
process.exit(1)
}
const { root, vitestPath, workspace } = await setupEnvironment()
const suitesToRun = getSuitesToRun(suites, root)
let isFirstRun = true
const { verify } = options
const runSuite = async () => {
try {
await buildVitest({ verify: isFirstRun && verify })
for (const suite of suitesToRun) {
await run(suite, {
verify: !!(isFirstRun && verify),
skipGit: !isFirstRun,
root,
vitestPath,
vitestMajor: parseVitestMajor(vitestPath),
workspace,
})
}
isFirstRun = false
return null
} catch (e) {
return e
}
}
await setupVitestRepo({ ...options, shallow: false })
const initialError = await runSuite()
if (initialError) {
await bisectVitest(options.good, runSuite)
} else {
console.log(`no errors for starting commit, cannot bisect`)
}
})
cli.help()
cli.parse()

async function run(suite: string, options: RunOptions) {
const { test } = await import(`./tests/${suite}.ts`)
await test({
...options,
workspace: path.resolve(options.workspace, suite),
})
}

function getSuitesToRun(suites: string[], root: string) {
let suitesToRun: string[] = suites
const availableSuites: string[] = fs
.readdirSync(path.join(root, 'tests'))
.filter((f: string) => !f.startsWith('_') && f.endsWith('.ts'))
.map((f: string) => f.slice(0, -3))
availableSuites.sort()
if (suitesToRun.length === 0) {
suitesToRun = availableSuites
} else {
const invalidSuites = suitesToRun.filter(
(x) => !x.startsWith('_') && !availableSuites.includes(x),
)
if (invalidSuites.length) {
console.log(`invalid suite(s): ${invalidSuites.join(', ')}`)
console.log(`available suites: ${availableSuites.join(', ')}`)
process.exit(1)
}
}
return suitesToRun
}
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "Vitest Ecosystem CI",
"scripts": {
"prepare": "pnpm exec simple-git-hooks",
"lint": "echo TODO",
"lint": "eslint --ignore-path .gitignore '**/*.ts'",
"lint:fix": "pnpm lint --fix",
"format": "prettier --ignore-path .gitignore --check .",
"format:fix": "pnpm format --write",
"test:self": "echo TODO",
"test": "echo TODO",
"bisect": "echo TODO"
"test:self": "tsx ecosystem-ci.ts _selftest",
"test": "tsx ecosystem-ci.ts",
"bisect": "tsx ecosystem-ci.ts bisect"
},
"simple-git-hooks": {
"pre-commit": "pnpm exec lint-staged --concurrent false"
Expand Down Expand Up @@ -38,16 +38,26 @@
"url": "https://github.com/vitest-dev/vitest-ecosystem-ci/issues"
},
"homepage": "https://github.com/vitest-dev/vitest-ecosystem-ci#readme",
"dependencies": {},
"dependencies": {
"@actions/core": "^1.10.0",
"cac": "^6.7.14",
"execa": "^7.1.1",
"node-fetch": "^3.3.1"
},
"devDependencies": {
"@antfu/ni": "^0.21.4",
"@types/node": "^18.16.18",
"@types/semver": "^7.5.0",
"@typescript-eslint/eslint-plugin": "^5.60.0",
"@typescript-eslint/parser": "^5.60.0",
"eslint": "^8.43.0",
"eslint-define-config": "^1.21.0",
"eslint-plugin-n": "^16.0.0",
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
"semver": "^7.5.2",
"simple-git-hooks": "^2.8.1",
"tsx": "^3.12.7",
"typescript": "^5.1.3"
}
}
Loading

0 comments on commit d3f4fdd

Please sign in to comment.