Skip to content

Commit

Permalink
test(cli): reject promise returned from exec() upon non-zero exit c…
Browse files Browse the repository at this point in the history
…ode (#5009)

fix(ci): remove noise/log statements from unit tests
  • Loading branch information
bjoerge committed Oct 19, 2023
1 parent d19aa5e commit a0f2c27
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ jobs:
run: |
node -v
npm -v
yarn test
yarn test --silent
env:
SANITY_CI_CLI_AUTH_TOKEN: ${{ secrets.SANITY_CI_CLI_AUTH_TOKEN }}
11 changes: 5 additions & 6 deletions packages/@sanity/cli/test/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ describeCliTest('CLI: `sanity preview`', () => {
expect(previewHtml).toContain('<h1>This is static.</h1>')
})

testConcurrent('start (hint for new `dev` command)', async () => {
const result = await runSanityCmdCommand('v3', ['start'], {expectFailure: true})
const error = result.stderr.trim()
expect(error).toContain('command is used to preview static builds')
expect(error).toContain('sanity dev')
expect(result.code).toBe(1)
testConcurrent('start (hint for new `dev` command)', () => {
return expect(runSanityCmdCommand('v3', ['start'])).rejects.toMatchObject({
stderr: /.*(command is used to preview static builds).*(sanity dev).*/,
code: 1,
})
})
})
})
51 changes: 40 additions & 11 deletions packages/@sanity/cli/test/shared/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,19 @@ export const getTestRunArgs = (version: string) => {
}
}

export async function runSanityCmdCommand(
export function runSanityCmdCommand(
version: string,
args: string[],
options: {env?: Record<string, string | undefined>; expectFailure?: boolean} = {},
options: {env?: Record<string, string | undefined>} = {},
): Promise<{
code: number | null
stdout: string
stderr: string
}> {
const result = await exec(process.argv[0], [cliBinPath, ...args], {
return exec(process.argv[0], [cliBinPath, ...args], {
cwd: path.join(studiosPath, version),
env: {...sanityEnv, ...options.env},
})

if (result.code !== 0 && !options.expectFailure) {
throw new Error(`Command failed with code ${result.code}. stderr: ${result.stderr}`)
}

return result
}

export function exec(
Expand All @@ -131,7 +125,7 @@ export function exec(
stdout: string
stderr: string
}> {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const stdout: Buffer[] = []
const stderr: Buffer[] = []

Expand All @@ -143,7 +137,42 @@ export function exec(
const stdoutStr = Buffer.concat(stdout).toString('utf8')
const stderrStr = Buffer.concat(stderr).toString('utf8')

resolve({code, stdout: stdoutStr, stderr: stderrStr})
if (code === 0) {
resolve({code, stdout: stdoutStr, stderr: stderrStr})
} else {
reject(
new ExecError(
`Command failed:
Exit code: ${code}
Command: ${command} ${args.join(' ')}
CWD: ${options.cwd}\n
--- stderr ---
${stderrStr}\n
--------------\n
${
stdoutStr &&
`--- stdout ---\n
${stdoutStr}\n
--------------\n`
}`,
code || 1,
stdoutStr,
stderrStr,
),
)
}
})
})
}

class ExecError extends Error {
code: number
stdout: string
stderr: string
constructor(message: string, code: number, stdout: string, stderr: string) {
super(message)
this.code = code
this.stdout = stdout
this.stderr = stderr
}
}

2 comments on commit a0f2c27

@vercel
Copy link

@vercel vercel bot commented on a0f2c27 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

performance-studio – ./

performance-studio.sanity.build
performance-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on a0f2c27 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.