Skip to content

Commit 3cfd8d5

Browse files
committed
Fix broken test configurations
1 parent f5ab175 commit 3cfd8d5

File tree

6 files changed

+138
-22
lines changed

6 files changed

+138
-22
lines changed

devserver/vite.config.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,24 @@ export default defineConfig(({ mode }) => {
6262
}
6363
},
6464
include: [
65+
'../build/tabs/*.js',
66+
'ace-builds',
67+
'ace-builds/src-noconflict/ace',
68+
'ace-builds/src-noconflict/ext-language_tools',
69+
'ace-builds/src-noconflict/ext-searchbox',
70+
'classnames',
71+
'js-slang/dist/createContext',
72+
'js-slang/dist/editors/ace/modes/source',
73+
'js-slang/dist/editors/ace/theme/source',
74+
'js-slang/dist/types',
75+
'js-slang/dist/utils/stringify',
76+
'react/jsx-dev-runtime',
77+
'react-ace',
78+
'react-ace/lib/ace',
79+
're-resizable',
6580
"vite-plugin-node-polyfills/shims/buffer",
6681
"vite-plugin-node-polyfills/shims/global",
6782
"vite-plugin-node-polyfills/shims/process",
68-
'../build/tabs/*.js',
69-
'react/jsx-dev-runtime'
7083
],
7184
},
7285
}

lib/buildtools/src/build/modules/commons.ts

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'fs/promises';
22
import { parse } from 'acorn';
33
import { generate } from 'astring';
4-
import type { BuildOptions as ESBuildOptions, OutputFile } from 'esbuild';
4+
import chalk from 'chalk';
5+
import type { BuildOptions as ESBuildOptions, OutputFile, Plugin as ESBuildPlugin } from 'esbuild';
56
import type es from 'estree';
67
import type { BuildResult, InputAsset } from '../../types.js';
78

@@ -26,12 +27,15 @@ export const commonEsbuildOptions = {
2627
} satisfies ESBuildOptions;
2728
// #endregion esbuildOptions
2829

29-
/**
30-
* Write the compiled output from ESBuild to the file system after performing AST transformation
31-
*/
32-
export async function outputBundleOrTab({ text }: OutputFile, input: InputAsset, outDir: string): Promise<BuildResult> {
33-
const parsed = parse(text, { ecmaVersion: 6 }) as es.Program;
30+
type ConvertAstResult = {
31+
severity: 'error',
32+
error: string
33+
} | {
34+
severity: 'success'
35+
output: es.Node
36+
};
3437

38+
function convertAst(parsed: es.Program): ConvertAstResult {
3539
// Account for 'use strict'; directives
3640
let declStatement: es.VariableDeclaration;
3741
if (parsed.body[0].type === 'VariableDeclaration') {
@@ -43,22 +47,18 @@ export async function outputBundleOrTab({ text }: OutputFile, input: InputAsset,
4347
const { init: callExpression } = declStatement.declarations[0];
4448
if (callExpression?.type !== 'CallExpression') {
4549
return {
46-
type: input.type,
4750
severity: 'error',
48-
input,
49-
errors: [`parse failure: Expected a CallExpression, got ${callExpression?.type ?? callExpression}`]
50-
} as BuildResult;
51+
error: `parse failure: Expected a CallExpression, got ${callExpression?.type ?? callExpression}`
52+
};
5153
}
5254

5355
const moduleCode = callExpression.callee;
5456

5557
if (moduleCode.type !== 'FunctionExpression' && moduleCode.type !== 'ArrowFunctionExpression') {
5658
return {
57-
type: input.type,
5859
severity: 'error',
59-
input,
60-
errors: [`${input.type} ${input.name} parse failure: Expected a function, got ${moduleCode.type}`]
61-
} as BuildResult;
60+
error: `parse failure: Expected a function, got ${moduleCode.type}`,
61+
};
6262
}
6363

6464
const output: es.ExportDefaultDeclaration = {
@@ -72,6 +72,29 @@ export async function outputBundleOrTab({ text }: OutputFile, input: InputAsset,
7272
}
7373
};
7474

75+
return {
76+
severity: 'success',
77+
output
78+
};
79+
}
80+
81+
/**
82+
* Write the compiled output from ESBuild to the file system after performing AST transformation
83+
*/
84+
export async function outputBundleOrTab({ text }: OutputFile, input: InputAsset, outDir: string): Promise<BuildResult> {
85+
const parsed = parse(text, { ecmaVersion: 6 }) as es.Program;
86+
87+
const astResult = convertAst(parsed);
88+
if (astResult.severity === 'error') {
89+
return {
90+
type: input.type,
91+
severity: 'error',
92+
input,
93+
errors: [`${input.type} ${input.name} ${astResult.error}`]
94+
} as BuildResult;
95+
}
96+
97+
const { output } = astResult;
7598
const outputDirectory = `${outDir}/${input.type}s`;
7699
await fs.mkdir(outputDirectory, { recursive: true });
77100

@@ -93,3 +116,30 @@ export async function outputBundleOrTab({ text }: OutputFile, input: InputAsset,
93116
await file?.close();
94117
}
95118
}
119+
120+
export function builderPlugin(input: InputAsset, outDir: string): ESBuildPlugin {
121+
return {
122+
name: 'Builder Plugin',
123+
async setup({ initialOptions, onEnd }) {
124+
if (initialOptions.write !== false) {
125+
throw new Error('Plugin must be used with write: false');
126+
}
127+
128+
const outpath = `${outDir}/${input.name}.js`;
129+
const file = await fs.open(outpath, 'w');
130+
const writeStream = file.createWriteStream();
131+
132+
onEnd(result => {
133+
const [{ text }] = result.outputFiles!;
134+
const parsed = parse(text, { ecmaVersion: 6 }) as es.Program;
135+
const astResult = convertAst(parsed);
136+
if (astResult.severity === 'success') {
137+
generate(astResult.output, { output: writeStream });
138+
console.log(chalk.greenBright(`Output written to ${outpath}`));
139+
} else {
140+
console.error(chalk.redBright(astResult.error));
141+
}
142+
});
143+
}
144+
};
145+
}

lib/buildtools/src/commands/__tests__/testing.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getTestCommand } from '../testing.js';
66
import { getCommandRunner } from './testingUtils.js';
77

88
vi.mock(import('../../getGitRoot.js'));
9-
vi.spyOn(process, 'cwd').mockReturnValue('/');
9+
vi.spyOn(process, 'cwd').mockReturnValue(testMocksDir);
1010
vi.spyOn(runner, 'runVitest').mockResolvedValue();
1111

1212
describe('Test regular test command', () => {
@@ -63,4 +63,49 @@ describe('Test regular test command', () => {
6363
const projectPath = `${testMocksDir}/..`;
6464
await expect(runCommand('--project', projectPath)).commandExit();
6565
});
66+
67+
test('--no-allow-only should not allow only :)', async () => {
68+
const mockConfig: configs.GetTestConfigurationResult = {
69+
severity: 'success',
70+
config: {
71+
test: {
72+
name: 'Test0'
73+
}
74+
}
75+
};
76+
mockedTestConfiguration.mockResolvedValueOnce(mockConfig);
77+
78+
await expect(runCommand('--no-allow-only')).commandSuccess();
79+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith(
80+
'test',
81+
[],
82+
[mockConfig.config],
83+
{ allowOnly: false }
84+
);
85+
});
86+
87+
test('--no-allow-only should be true when CI', async () => {
88+
vi.stubEnv('CI', 'yeet');
89+
try {
90+
const mockConfig: configs.GetTestConfigurationResult = {
91+
severity: 'success',
92+
config: {
93+
test: {
94+
name: 'Test0'
95+
}
96+
}
97+
};
98+
mockedTestConfiguration.mockResolvedValueOnce(mockConfig);
99+
100+
await expect(runCommand()).commandSuccess();
101+
expect(runner.runVitest).toHaveBeenCalledExactlyOnceWith(
102+
'test',
103+
[],
104+
[mockConfig.config],
105+
{ allowOnly: false }
106+
);
107+
} finally {
108+
vi.unstubAllEnvs();
109+
}
110+
});
66111
});

lib/buildtools/src/commands/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Command } from '@commander-js/extra-typings';
22
import { buildAll } from '../build/all.js';
33
import { buildHtml, buildSingleBundleDocs } from '../build/docs/index.js';
44
import { formatResult, formatResultObject } from '../build/formatter.js';
5-
import { buildManifest, resolveAllBundles, resolveEitherBundleOrTab, resolveSingleBundle , resolveSingleTab } from '../build/manifest.js';
5+
import { buildManifest, resolveAllBundles, resolveEitherBundleOrTab, resolveSingleBundle, resolveSingleTab } from '../build/manifest.js';
66
import { buildBundle, buildTab } from '../build/modules/index.js';
77
import { getBundlesDir, getOutDir } from '../getGitRoot.js';
88
import { runBuilderWithPrebuild } from '../prebuild/index.js';

lib/buildtools/src/commands/testing.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ const vitestModeOption = new Option('--mode <mode>', 'Vitest Run Mode. See https
1414
const watchOption = new Option('-w, --watch', 'Run tests in watch mode');
1515
const updateOption = new Option('-u, --update', 'Update snapshots');
1616
const coverageOption = new Option('--coverage');
17-
const allowOnlyOption = new Option('--no-allow-only', 'Allow the use of .only')
18-
.default(!process.env.CI);
1917

2018
export const getTestCommand = () => new Command('test')
2119
.description('Run test for the specific bundle or tab at the specified directory.')
2220
.addOption(vitestModeOption)
2321
.addOption(watchOption)
2422
.addOption(updateOption)
2523
.addOption(coverageOption)
26-
.addOption(allowOnlyOption)
24+
.option('--no-allow-only', 'Allow the use of .only in tests', !process.env.CI)
2725
.option('-p, --project <directory>', 'Path to the directory that is the root of your test project')
2826
.argument('[patterns...]', 'Test patterns to filter by.')
2927
.action(async (patterns, { mode, project, ...options }) => {
@@ -58,7 +56,7 @@ export const getTestAllCommand = () => new Command('testall')
5856
.addOption(watchOption)
5957
.addOption(updateOption)
6058
.addOption(coverageOption)
61-
.addOption(allowOnlyOption)
59+
.option('--no-allow-only', 'Allow the use of .only in tests', !process.env.CI)
6260
.action(async (patterns, { mode, ...options }) => {
6361
const configs = await getAllTestConfigurations(!!options.watch);
6462
if (configs.length === 0) {

lib/buildtools/src/testing/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ export function setBrowserOptions(indivConfig: TestProjectInlineConfiguration, w
3131
}
3232
});
3333
indivConfig.test!.include = [];
34+
35+
if (!indivConfig.optimizeDeps) {
36+
indivConfig.optimizeDeps = {};
37+
}
38+
39+
if (indivConfig.optimizeDeps.include === undefined) {
40+
indivConfig.optimizeDeps.include = [];
41+
}
42+
43+
indivConfig.optimizeDeps.include.push(...sharedTabsConfig.optimizeDeps.include);
3444
}
3545
}
3646

0 commit comments

Comments
 (0)