Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create dir for output file #1242

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.

import { Builders, Enums, Factories, Serialize, Spec, Validation } from '@cyclonedx/cyclonedx-library'
import { Argument, Command, Option } from 'commander'
import { existsSync, openSync } from 'fs'
import { existsSync, mkdirSync, openSync } from 'fs'
import { dirname, resolve } from 'path'

import { loadJsonFile, writeAllSync } from './_helpers'
Expand Down Expand Up @@ -315,7 +315,11 @@ export async function run (process: NodeJS.Process): Promise<number> {
}
}
}

const directory = dirname(options.outputFile)
if (!existsSync(directory)) {
jkowalleck marked this conversation as resolved.
Show resolved Hide resolved
myConsole.info('INFO | creating directory', directory)
mkdirSync(directory, { recursive: true })
}
myConsole.log('LOG | writing BOM to', options.outputFile)
const written = await writeAllSync(
options.outputFile === OutputStdOut
Expand Down
11 changes: 10 additions & 1 deletion tests/_helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,17 @@ function getNpmVersion () {
return v
}

/**
* @param {string} s
* @return {string}
*/
function regexEscape (s) {
return s.replace(/[\^$(){}[\]+*?.|\\-]/g, '\\$&')
}

module.exports = {
hashFile,
makeReproducible,
getNpmVersion
getNpmVersion,
regexEscape
}
25 changes: 14 additions & 11 deletions tests/integration/cli.from-setups.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
*/

const { spawnSync } = require('child_process')
const { join } = require('path')
const { writeFileSync, readFileSync, mkdirSync } = require('fs')
const { dirname, join } = require('path')
const { writeFileSync, readFileSync, existsSync } = require('fs')

const { describe, expect, test } = require('@jest/globals')

const { makeReproducible, getNpmVersion } = require('../_helper')
const { makeReproducible, getNpmVersion, regexEscape } = require('../_helper')
const { UPDATE_SNAPSHOTS, mkTemp, cliWrapper, latestCdxSpecVersion, dummyProjectsRoot, dummyResultsRoot, projectDemoRootPath, demoResultsRoot } = require('./')

describe('integration.cli.from-setups', () => {
Expand All @@ -37,9 +37,6 @@ describe('integration.cli.from-setups', () => {
const formats = ['json', 'xml']

describe('dummy_projects', () => {
const tmpRootRun = join(tmpRoot, 'with-prepared')
mkdirSync(tmpRootRun)

const useCases = [
{
subject: 'bare',
Expand All @@ -60,7 +57,9 @@ describe('integration.cli.from-setups', () => {

function runTest (subject, project, format, additionalCliArgs = []) {
const expectedOutSnap = join(dummyResultsRoot, subject, `${project}.snap.${format}`)
const outFile = join(tmpRoot, `${subject}_${project}.${format}`)
const outFile = join(tmpRoot, subject, `${project}.${format}`)
const outDirExisted = existsSync(dirname(outFile))
// no need to create that outFile dir first - the tool is expected to do that for us
const res = spawnSync(
process.execPath,
['--', cliWrapper,
Expand All @@ -69,10 +68,11 @@ describe('integration.cli.from-setups', () => {
'--output-format', format,
'--output-reproducible',
'--output-file', outFile,
'--validate'
'--validate',
'-vvv'
], {
cwd: join(dummyProjectsRoot, project),
stdio: ['ignore', 'inherit', 'pipe'],
stdio: ['ignore', 'ignore', 'pipe'],
encoding: 'utf8'
}
)
Expand All @@ -84,6 +84,11 @@ describe('integration.cli.from-setups', () => {
process.stderr.write('\n')
throw err
}

const expectStdErr = expect(res.stderr);
(outDirExisted ? expectStdErr.not : expectStdErr).toContain(`creating directory ${dirname(outFile)}`)
expectStdErr.toMatch(new RegExp(`wrote \\d+ bytes to ${regexEscape(outFile)}`))

const actualOutput = makeReproducible(format, readFileSync(outFile, 'utf8'))

if (UPDATE_SNAPSHOTS) {
Expand All @@ -97,8 +102,6 @@ describe('integration.cli.from-setups', () => {
}

describe.each(useCases)('subject: $subject', (ud) => {
mkdirSync(join(tmpRootRun, ud.subject))

describe.each(ud.dummyProject)('dummyProject: %s', (dummyProject) => {
describe.each(formats)('format: %s', (format) => {
(skipAllTests
Expand Down
Loading