Skip to content

Commit 3f42bac

Browse files
committed
Fixing an issue in the run command and updating the unit, integration, and e2e tests.
1 parent e2e5508 commit 3f42bac

8 files changed

Lines changed: 133 additions & 68 deletions

File tree

src/commands/run.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class RunCommand extends BaseCommand {
3838

3939
// Initialize command to default npm run
4040
const finalCmd = 'npm';
41-
const finalArgs = ['run', script, '--', ...scriptArgs];
41+
let finalArgs = null;
4242
let execPath = null;
4343

4444
// Check for module:script syntax
@@ -64,6 +64,7 @@ export default class RunCommand extends BaseCommand {
6464
this.error(`Failed to find package.json for module ${moduleName}`);
6565
return;
6666
}
67+
finalArgs = ['run', scriptName, '--', ...scriptArgs];
6768
execPath = modulePath;
6869

6970
} else {
@@ -73,6 +74,7 @@ export default class RunCommand extends BaseCommand {
7374
this.error(`Script ${script} does not exist in Astrical core`);
7475
return;
7576
}
77+
finalArgs = ['run', script, '--', ...scriptArgs];
7678
execPath = siteDir;
7779
}
7880

test/e2e/lifecycle.e2e.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,24 @@ if (args[0] === 'build') {
4949
dependencies: {
5050
// Use file: protocol to point to local mock
5151
'astro': `file:${mockAstroDir}`
52+
},
53+
scripts: {
54+
'build': 'astro build',
55+
'dev': 'astro dev',
56+
'preview': 'astro preview'
5257
}
5358
}),
5459
'README.md': '# E2E Starter',
5560
'astrical.yml': 'name: e2e-test\nversion: 0.0.1', // ESSENTIAL for CLI to recognize project
5661
'src/pages/index.astro': '--- ---',
5762
'src/core/index.ts': '// core',
58-
'src/core/package.json': '{}'
63+
'src/core/package.json': JSON.stringify({
64+
scripts: {
65+
'build': 'astro build',
66+
'dev': 'astro dev',
67+
'preview': 'astro preview'
68+
}
69+
})
5970
});
6071

6172
// 3. Setup Mock Module Repo

test/integration/commands/build.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('BuildCommand Integration', () => {
7474
// 2. Verify exec called with local binary
7575
// BuildCommand uses runCommand -> exec
7676
expect(execMock).toHaveBeenCalledWith(
77-
expect.stringContaining('node_modules/.bin/astro build'),
77+
expect.stringContaining('npm run build'),
7878
expect.objectContaining({
7979
cwd: expect.stringContaining('_site')
8080
}),

test/integration/commands/clean.integration.test.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ describe('CleanCommand Integration', () => {
1111
beforeEach(async () => {
1212
tempDir = await createTempDir('clean-integration-');
1313
// Setup initial state
14-
await fs.ensureDir(path.join(tempDir, 'dist'));
15-
await fs.outputFile(path.join(tempDir, 'dist', 'index.js'), 'console.log("hi")');
14+
await fs.ensureDir(path.join(tempDir, '_site'));
15+
await fs.outputFile(path.join(tempDir, '_site', 'index.html'), '<html></html>');
1616
await fs.ensureDir(path.join(tempDir, 'node_modules'));
1717
await fs.outputFile(path.join(tempDir, 'node_modules', 'pkg.json'), '{}');
1818
});
@@ -21,27 +21,18 @@ describe('CleanCommand Integration', () => {
2121
if (tempDir) await fs.remove(tempDir);
2222
});
2323

24-
it('should remove dist and node_modules directories', async () => {
24+
it('should remove _site and node_modules directories', async () => {
2525
const cli = new CLI({ commandName: 'astrical' });
2626
const command = new CleanCommand(cli);
27-
// CleanCommand usually runs in process.cwd(), so we need to mock that or pass directory if supported.
28-
// Checking clean.ts implementation... it uses process.cwd().
29-
// We can spy spy process.cwd() or similar.
30-
31-
// Wait, does CleanCommand accept a directory argument?
32-
// Let's check implementation. If not, we have to change process.cwd.
3327

3428
const originalCwd = process.cwd();
3529
try {
3630
process.chdir(tempDir);
3731

38-
await command.run();
39-
40-
expect(fs.existsSync(path.join(tempDir, 'dist'))).toBe(false);
41-
// clean command removes node_modules/.vite, not the whole node_modules
42-
expect(fs.existsSync(path.join(tempDir, 'node_modules', '.vite'))).toBe(false);
43-
expect(fs.existsSync(path.join(tempDir, 'node_modules'))).toBe(true);
32+
await command.run({});
4433

34+
expect(fs.existsSync(path.join(tempDir, '_site'))).toBe(false);
35+
expect(fs.existsSync(path.join(tempDir, 'node_modules'))).toBe(false);
4536
} finally {
4637
process.chdir(originalCwd);
4738
}

test/integration/commands/dev_preview.integration.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PreviewCommand from '../../../src/commands/preview.js';
55
import { createTempDir } from '../../utils/integration-helpers.js';
66
import path from 'node:path';
77
import fs from 'fs-extra';
8-
import { spawn } from 'child_process';
8+
import { spawn, exec } from 'child_process';
99
import EventEmitter from 'events';
1010

1111
vi.mock('child_process', () => ({
@@ -32,6 +32,12 @@ describe('Dev/Preview Integration', () => {
3232
setTimeout(() => child.emit('close', 0), 10);
3333
return child;
3434
});
35+
36+
vi.mocked(exec).mockImplementation(((cmd: string, options: any, cb: any) => {
37+
const callback = cb || options;
38+
callback(null, { stdout: '', stderr: '' });
39+
return {} as any;
40+
}) as any);
3541
});
3642

3743
afterEach(() => {

test/integration/commands/run.integration.test.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,26 @@ describe('RunCommand Integration', () => {
2222

2323
// Setup minimal env
2424
await fs.ensureDir(path.join(projectDir, 'src', 'core'));
25-
26-
// Setup a module with a script
25+
await fs.outputFile(path.join(projectDir, 'src', 'core', 'package.json'), JSON.stringify({
26+
name: 'astrical-core',
27+
scripts: {
28+
'test-script': 'echo test'
29+
}
30+
}));
2731
await fs.ensureDir(path.join(projectDir, 'src', 'modules', 'my-auth'));
2832
await fs.outputFile(path.join(projectDir, 'src', 'modules', 'my-auth', 'package.json'), JSON.stringify({
2933
scripts: {
3034
'seed': 'node seed.js'
3135
}
3236
}));
3337

38+
await fs.ensureDir(path.join(projectDir, '_site'));
39+
await fs.outputFile(path.join(projectDir, '_site', 'package.json'), JSON.stringify({
40+
scripts: {
41+
'test-script': 'echo test'
42+
}
43+
}));
44+
3445
spawnMock = vi.mocked(spawn).mockImplementation(() => {
3546
const child: any = new EventEmitter();
3647
child.stdout = new EventEmitter();
@@ -55,7 +66,7 @@ describe('RunCommand Integration', () => {
5566
const command = new RunCommand(cli);
5667
Object.assign(command, { projectRoot: projectDir });
5768

58-
await command.run('test-script', '--flag', {}); // Add options object
69+
await command.run({ script: 'test-script', args: ['--flag'] });
5970

6071
expect(spawnMock).toHaveBeenCalledWith(
6172
'npm',
@@ -71,18 +82,15 @@ describe('RunCommand Integration', () => {
7182
const command = new RunCommand(cli);
7283
Object.assign(command, { projectRoot: projectDir });
7384

74-
await command.run('my-auth:seed', '--force', {}); // Add options object
75-
76-
// Module scripts run using sh -c or cmd /c
77-
const expectedCmd = process.platform === 'win32' ? 'cmd' : 'sh';
78-
const expectedArgs = process.platform === 'win32'
79-
? ['/c', 'node seed.js --force']
80-
: ['-c', 'node seed.js --force'];
85+
await command.run({ script: 'my-auth:seed', args: ['--force'] });
8186

87+
// Module scripts run via npm run scriptName inside module dir
8288
expect(spawnMock).toHaveBeenCalledWith(
83-
expectedCmd,
84-
expectedArgs,
85-
expect.anything()
89+
'npm',
90+
['run', 'seed', '--', '--force'],
91+
expect.objectContaining({
92+
cwd: expect.stringContaining(path.join('modules', 'my-auth'))
93+
})
8694
);
8795
});
8896
});

test/unit/commands/build.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('BuildCommand', () => {
7676
expect(copyEnvironment).toHaveBeenCalledWith('/mock/root');
7777

7878
expect(runCommand).toHaveBeenCalledWith(
79-
expect.stringContaining('/mock/root/node_modules/.bin/astro build'),
79+
'npm run build',
8080
expect.stringContaining('_site')
8181
);
8282

0 commit comments

Comments
 (0)