Skip to content

Commit

Permalink
fix: Valibot schema logic & better test coverage (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts committed Dec 18, 2023
1 parent a24485f commit 08ad44e
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 271 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-meals-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"secco": patch
---

After updating `valibot` to the latest version, the validation for environment variables wasn't behaving correctly. The custom validation is fixed now.
4 changes: 2 additions & 2 deletions integration/__tests__/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const warning = '.seccorc file already exists in this directory'

describe('init', () => {
it('should work in directory without .seccorc file', () => {
const [exitCode, logs] = SeccoCLI.from('empty').invoke(['init'])
const [exitCode, logs] = SeccoCLI().setCwd('empty').invoke(['init'])

logs.should.contain(initQuestion)
logs.should.not.contain(warning)
expect(exitCode).toBe(0)
})
it('should warn in directory with .seccorc file', () => {
const [exitCode, logs] = SeccoCLI.from('existing-config-file').invoke(['init'])
const [exitCode, logs] = SeccoCLI().setCwd('existing-config-file').invoke(['init'])

logs.should.contain(initQuestion)
logs.should.contain(warning)
Expand Down
34 changes: 34 additions & 0 deletions integration/__tests__/missing-information.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { join } from 'node:path'
import { SeccoCLI } from '../helpers/invoke-cli'

const missingSourcePackagesLocation = join(__dirname, '..', 'fixtures', 'missing-source-packages')

describe('missing .seccorc', () => {
it('should display error when no .seccorc or env var is found', () => {
const [exitCode, logs] = SeccoCLI().setCwd('empty').invoke([''])

logs.should.contain('No `.seccorc` file found in')
logs.should.contain('Please run `secco init` to create a new `.seccorc` file.')
expect(exitCode).toBe(0)
})
})

describe('missing package.json', () => {
it('should display error when no package.json is found', () => {
const [exitCode, logs] = SeccoCLI().setCwd('existing-config-file').invoke([''])

logs.should.contain('No `package.json` found in')
logs.should.contain('Current directory must contain a `package.json` file.')
expect(exitCode).toBe(0)
})
})

describe('missing source packages', () => {
it('should display error when no source package is found in package.json', () => {
const [exitCode, logs] = SeccoCLI().setCwd('missing-source-packages').setEnv({ SECCO_SOURCE_PATH: missingSourcePackagesLocation }).invoke([''])

logs.should.contain(`You haven't got any source dependencies in your current \`package.json\`.`)
logs.should.contain(`If you only want to use \`secco\` you'll need to add the dependencies to your \`package.json\`.`)
expect(exitCode).toBe(0)
})
})
9 changes: 9 additions & 0 deletions integration/fixtures/missing-source-packages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "missing-source-packages",
"version": "1.0.0",
"private": true,
"packageManager": "npm@9.6.7",
"dependencies": {
"say-hello-world": "^1.0.0"
}
}
52 changes: 51 additions & 1 deletion integration/helpers/invoke-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,58 @@ const fixturesLocation = join(__dirname, '..', 'fixtures')
type CreateLogsMatcherReturn = ReturnType<typeof createLogsMatcher>
type InvokeResult = [exitCode: number, logsMatcher: CreateLogsMatcherReturn]

export function SeccoCLI() {
let env: Record<string, string> = {}
let cwd = ''

const self = {
setEnv: (_env: Record<string, string>) => {
env = _env
return self
},
setCwd: (_cwd: string) => {
cwd = _cwd
return self
},
invoke: (args: Array<string>): InvokeResult => {
const NODE_ENV = 'production'

try {
const results = execaSync(
process.execPath,
[builtCliLocation].concat(args),
{
cwd: join(fixturesLocation, cwd),
env: { NODE_ENV, ...env },
},
)
return [
results.exitCode,
createLogsMatcher(strip(results.stderr.toString() + results.stdout.toString())),
]
}
catch (e) {
const execaError = e as ExecaSyncError
return [
execaError.exitCode,
createLogsMatcher(strip(execaError.stdout?.toString() || ``)),
]
}
},
}

return self
}

/*
export const SeccoCLI = {
// Set environment variables
setEnv(env: Record<string, string>): void {
customEnv = env
},
from(relativeCwd: string) {
return {
// Run the CLI with the given arguments
invoke(args: Array<string>): InvokeResult {
const NODE_ENV = 'production'
Expand All @@ -23,7 +72,7 @@ export const SeccoCLI = {
[builtCliLocation].concat(args),
{
cwd: join(fixturesLocation, relativeCwd),
env: { NODE_ENV },
env: { NODE_ENV, ...customEnv },
},
)
return [
Expand All @@ -42,3 +91,4 @@ export const SeccoCLI = {
}
},
}
*/
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"pathe": "^1.1.1",
"rc9": "^2.1.1",
"signal-exit": "^4.1.0",
"valibot": "^0.22.0",
"valibot": "^0.24.1",
"verdaccio": "^5.29.0",
"yargs": "^17.7.2"
},
Expand All @@ -87,7 +87,7 @@
"strip-ansi": "^7.1.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vitest": "^0.34.6"
"vitest": "^1.0.4"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
Expand Down
Loading

0 comments on commit 08ad44e

Please sign in to comment.