-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f81ac0c
commit e398310
Showing
11 changed files
with
549 additions
and
1,517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
ExecException, | ||
execSync, | ||
ExecSyncOptionsWithBufferEncoding, | ||
} from 'child_process' | ||
import path, { join } from 'path' | ||
import fs, { mkdtempSync } from 'fs' | ||
import os from 'os' | ||
|
||
export type ExecError = ExecException & { stderr: string; stdout: string } | ||
|
||
export type ExecOptions = ExecSyncOptionsWithBufferEncoding & { | ||
silent?: boolean | ||
} | ||
|
||
export type ExecResult = { | ||
code: number | ||
stdout?: string | ||
stderr?: string | ||
error?: ExecError | ||
} | ||
|
||
const TEST_DATA_DIR = path.join(process.cwd(), 'test', 'commands-data') | ||
|
||
/** | ||
* Utility test class to execute the CLI command | ||
*/ | ||
export class CLIExecutor { | ||
private binPath: string | ||
|
||
constructor() { | ||
const curentDir = process.cwd() | ||
this.binPath = | ||
process.platform === 'win32' | ||
? path.join(curentDir, 'bin', 'dev.cmd') | ||
: path.join(curentDir, 'bin', 'dev.js') | ||
} | ||
|
||
public execCommand(cmd: string, options?: ExecOptions): Promise<ExecResult> { | ||
const silent = options?.silent ?? true | ||
const command = `${this.binPath} ${cmd}` | ||
const cwd = process.cwd() | ||
|
||
return new Promise((resolve) => { | ||
if (silent) { | ||
try { | ||
const r = execSync(command, { ...options, stdio: 'pipe', cwd }) | ||
Check warning Code scanning / CodeQL Shell command built from environment values Medium test
This shell command depends on an uncontrolled
absolute path Error loading related location Loading |
||
const stdout = r.toString() | ||
|
||
resolve({ code: 0, stdout }) | ||
} catch (error) { | ||
const err = error as ExecError | ||
console.log(error) | ||
|
||
resolve({ | ||
code: 1, | ||
error: err, | ||
stdout: err.stdout?.toString() ?? '', | ||
stderr: err.stderr?.toString() ?? '', | ||
}) | ||
} | ||
} else { | ||
execSync(command, { ...options, stdio: 'inherit', cwd }) | ||
Check warning Code scanning / CodeQL Shell command built from environment values Medium test
This shell command depends on an uncontrolled
absolute path Error loading related location Loading |
||
resolve({ code: 0 }) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// On macOS, os.tmpdir() is not a real path: | ||
// https://github.com/nodejs/node/issues/11422 | ||
const TMP_DIR = fs.realpathSync(os.tmpdir()) | ||
|
||
export async function runInDir( | ||
dir: 'tmp' | string, | ||
fn: () => Promise<void>, | ||
): Promise<void> { | ||
const baseCwd = process.cwd() | ||
const ctd = | ||
dir === 'tmp' | ||
? mkdtempSync(path.join(TMP_DIR, 'publicodes-cli-test-')) | ||
: path.join(TEST_DATA_DIR, dir) | ||
|
||
if (!fs.existsSync(ctd)) { | ||
fs.mkdirSync(ctd, { recursive: true }) | ||
} | ||
|
||
process.chdir(ctd) | ||
|
||
try { | ||
await fn() | ||
} finally { | ||
process.chdir(baseCwd) | ||
if (dir === 'tmp' && fs.existsSync(ctd)) { | ||
fs.rmSync(ctd, { recursive: true }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "yarn-init", | ||
"version": "1.0.0", | ||
"description": "", | ||
"author": "", | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"files": [ | ||
"dist" | ||
], | ||
"peerDependencies": { | ||
"publicodes": "^1.5.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CLIExecutor, runInDir } from '../cli-utils' | ||
|
||
describe('publicodes --help', () => { | ||
it('should list all available commands', async () => { | ||
const cli = new CLIExecutor() | ||
|
||
runInDir('tmp', async () => { | ||
const { stdout } = await cli.execCommand('--help') | ||
|
||
expect(stdout).toContain('init initialize a new project') | ||
Check failure on line 10 in test/commands/base.test.ts GitHub Actions / mainUnhandled error
|
||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { execSync } from 'child_process' | ||
import { CLIExecutor, runInDir } from '../cli-utils' | ||
import fs from 'fs' | ||
|
||
describe('publicodes init', () => { | ||
it('should update existing package.json', async () => { | ||
const cli = new CLIExecutor() | ||
|
||
runInDir('tmp', async () => { | ||
execSync('yarn init -y') | ||
const { stdout } = await cli.execCommand('init') | ||
|
||
expect(stdout).toContain('existing package.json file') | ||
Check failure on line 13 in test/commands/init.test.ts GitHub Actions / mainUnhandled error
|
||
expect(stdout).toContain('package.json file written') | ||
expect(stdout).toContain('🚀 publicodes is ready to use!') | ||
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8')) | ||
expect(packageJson).toMatchObject({ | ||
type: 'module', | ||
main: 'dist/index.js', | ||
types: 'dist/index.d.ts', | ||
files: ['dist'], | ||
peerDependencies: { | ||
publicodes: '^1.5.1', | ||
}, | ||
devDependencies: { | ||
'@publicodes/tools': '^1.5.1', | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.