Skip to content

Commit

Permalink
test: fix env variables in exec helper under windows
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Jan 9, 2025
1 parent ea4604d commit d09f2b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/color-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export const getColorSpace = (mockThis) => {
// runtime values supported colors: `nodejs`, `edge`, `experimental-edge`

// whether the output is supported
/*@__INLINE__*/
let isTTY = isPM2 || env.NEXT_RUNTIME?.includes('edge') || (isDeno ? Deno.isatty(1) : !!proc.stdout?.isTTY);

// enforce a specific color support:
Expand Down
14 changes: 7 additions & 7 deletions test/flags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('enable colors', () => {

test(`FORCE_COLOR=true`, () => {
const filename = path.join(TEST_PATH, './cli/output.js');
const received = execScriptSync(filename, [], ['FORCE_COLOR=true']);
const received = execScriptSync(filename, [], { FORCE_COLOR: true });
const expected =
'\x1b[31mred\x1b[39m|\x1b[38;2;80;80;80mrgb\x1b[39m|\x1b[48;2;80;80;80mbgRgb\x1b[49m|\x1b[38;2;255;255;255mhex\x1b[39m|\x1b[48;2;255;255;255mbgHex\x1b[49m';

Expand All @@ -49,23 +49,23 @@ describe('enable colors', () => {

test(`FORCE_COLOR=1`, () => {
const filename = path.join(TEST_PATH, './cli/output.js');
const received = execScriptSync(filename, [], ['FORCE_COLOR=1']);
const received = execScriptSync(filename, [], { FORCE_COLOR: 1 });
const expected = '\x1b[31mred\x1b[39m|\x1b[30mrgb\x1b[39m|\x1b[40mbgRgb\x1b[49m|\x1b[97mhex\x1b[39m|\x1b[107mbgHex\x1b[49m';

expect(esc(received)).toEqual(esc(expected));
});

test(`FORCE_COLOR=2`, () => {
const filename = path.join(TEST_PATH, './cli/output.js');
const received = execScriptSync(filename, [], ['FORCE_COLOR=2']);
const received = execScriptSync(filename, [], { FORCE_COLOR: 2 });
const expected = '\x1b[31mred\x1b[39m|\x1b[38;5;239mrgb\x1b[39m|\x1b[48;5;239mbgRgb\x1b[49m|\x1b[38;5;231mhex\x1b[39m|\x1b[48;5;231mbgHex\x1b[49m';

expect(esc(received)).toEqual(esc(expected));
});

test(`FORCE_COLOR=3`, () => {
const filename = path.join(TEST_PATH, './cli/output.js');
const received = execScriptSync(filename, [], ['FORCE_COLOR=3']);
const received = execScriptSync(filename, [], { FORCE_COLOR: 3 });
const expected =
'\x1b[31mred\x1b[39m|\x1b[38;2;80;80;80mrgb\x1b[39m|\x1b[48;2;80;80;80mbgRgb\x1b[49m|\x1b[38;2;255;255;255mhex\x1b[39m|\x1b[48;2;255;255;255mbgHex\x1b[49m';

Expand All @@ -77,23 +77,23 @@ describe('disable colors', () => {
test(`--no-color`, () => {
const filename = path.join(TEST_PATH, './cli/output.js');
// flags has priority over env variable
const received = execScriptSync(filename, ['--no-color'], ['FORCE_COLOR=1']);
const received = execScriptSync(filename, ['--no-color'], { FORCE_COLOR: 1 });
const expected = 'red|rgb|bgRgb|hex|bgHex';
expect(esc(received)).toEqual(esc(expected));
});

test(`--color=false`, () => {
const filename = path.join(TEST_PATH, './cli/output.js');
// flags has priority over env variable
const received = execScriptSync(filename, ['--color=false'], ['FORCE_COLOR=1']);
const received = execScriptSync(filename, ['--color=false'], { FORCE_COLOR: 1 });
const expected = 'red|rgb|bgRgb|hex|bgHex';
expect(esc(received)).toEqual(esc(expected));
});

test(`--color=never`, () => {
const filename = path.join(TEST_PATH, './cli/output.js');
// flags has priority over env variable
const received = execScriptSync(filename, ['--color=never'], ['FORCE_COLOR=1']);
const received = execScriptSync(filename, ['--color=never'], { FORCE_COLOR: 1 });
const expected = 'red|rgb|bgRgb|hex|bgHex';
expect(esc(received)).toEqual(esc(expected));
});
Expand Down
56 changes: 16 additions & 40 deletions test/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,54 +45,30 @@ export const readTextFileSync = (file) => {
*
* @param {string} file The file path to execute.
* @param {Array<string>} flags The CLI flags.
* @param {Array<string>} env The environment variables.
* @param {Object} env The environment variables.
* @return {string} CLI output as result of execution.
*/
export const execScriptSync = (file, flags = [], env = []) => {
let envVars = env.length ? '' + env.join(' ') + ' ' : '';

// set ENV variables on Windows, e.g.: `set FORCE_COLOR=true | node app.js`
if (isWin) {
envVars = [];
env.forEach((expression) => {
envVars.push(`set ${expression} | `);
export const execScriptSync = (file, flags = [], env = {}) => {
let output = '';

try {
output = execSync(`node ${file} ${flags.join(' ')}`, {
env: {
...process.env, // preserve existing environment variables
...env,
},
args: flags,
//stdio: 'inherit', // pass stdout/stderr directly to the console
});
}

const cmd = envVars + 'node ' + file + ' ' + flags.join(' ');
const result = execSync(cmd);
} catch (error) {
console.error('Error executing command:', error.message);
}

// replace last newline in result
return result.toString().replace(/\n$/, '');
return output.toString().replace(/\n$/, '');
};

// TODO: get colorized output in process.stdout using child_process.execSync
// export const execScriptOutSync = (file, flags = [], env = []) => {
// const envVars = env.length ? '' + env.join(' ') + ' ' : '';
// const cmd = envVars + 'node ' + file + ' ' + flags.join(' ');
//
// //const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {});
// const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {});
//
// const result = execSync(
// cmd,
// // reset stdio to enable isTTY for colorized output
// { stdio: 'inherit' },
// //{ stdio: [0, process.stdout, process.stderr] },
// //{ stdio: [1] },
// );
//
// const { calls } = stdout.mock;
// let output = calls.length > 0 ? calls[0][0] : '';
//
// stdout.mockClear();
// stdout.mockRestore();
// console.log('--> ', { cmd, output, calls, stdout: stdout.mock }, stdout);
// //console.log('--> ', result.toString());
//
// return output;
// };

export const getCompareFileContents = function(
receivedFile,
expectedFile,
Expand Down

0 comments on commit d09f2b7

Please sign in to comment.