Files
alepm/tests/run-command.test.js
ale 2796185f36 1.0.3
Signed-off-by: ale <ale@manalejandro.com>
2025-08-19 04:39:40 +02:00

136 líneas
3.7 KiB
JavaScript

const fs = require('fs-extra');
const path = require('path');
const PackageManager = require('../src/core/package-manager');
describe('Run Command', () => {
let tempDir;
let packageManager;
beforeEach(async () => {
tempDir = path.join(__dirname, 'temp-run-test');
await fs.ensureDir(tempDir);
packageManager = new PackageManager({ projectRoot: tempDir });
});
afterEach(async () => {
if (await fs.pathExists(tempDir)) {
await fs.remove(tempDir);
}
});
test('should run a simple script successfully', async () => {
const packageJson = {
name: 'test-package',
version: '1.0.0',
scripts: {
hello: 'echo "Hello World"'
}
};
await fs.writeJson(path.join(tempDir, 'package.json'), packageJson);
// This should not throw
await packageManager.runScript('hello');
});
test('should fail when script does not exist', async () => {
const packageJson = {
name: 'test-package',
version: '1.0.0',
scripts: {
hello: 'echo "Hello World"'
}
};
await fs.writeJson(path.join(tempDir, 'package.json'), packageJson);
await expect(packageManager.runScript('nonexistent')).rejects.toThrow(
'Script "nonexistent" not found'
);
});
test('should not fail when script does not exist with ifPresent option', async () => {
const packageJson = {
name: 'test-package',
version: '1.0.0',
scripts: {
hello: 'echo "Hello World"'
}
};
await fs.writeJson(path.join(tempDir, 'package.json'), packageJson);
// This should not throw
await packageManager.runScript('nonexistent', { ifPresent: true });
});
test('should fail when package.json does not exist', async () => {
// Override ensureInitialized to prevent auto-creation
const originalEnsureInitialized = packageManager.ensureInitialized;
packageManager.ensureInitialized = async () => {};
await expect(packageManager.runScript('hello')).rejects.toThrow(
'No package.json found in current directory'
);
// Restore original method
packageManager.ensureInitialized = originalEnsureInitialized;
});
test('should fail when package.json has no scripts section', async () => {
const packageJson = {
name: 'test-package',
version: '1.0.0'
};
await fs.writeJson(path.join(tempDir, 'package.json'), packageJson);
await expect(packageManager.runScript('hello')).rejects.toThrow(
'No scripts section found in package.json'
);
});
test('should not fail when package.json has no scripts section with ifPresent option', async () => {
const packageJson = {
name: 'test-package',
version: '1.0.0'
};
await fs.writeJson(path.join(tempDir, 'package.json'), packageJson);
// This should not throw
await packageManager.runScript('hello', { ifPresent: true });
});
test('should run script with silent option', async () => {
const packageJson = {
name: 'test-package',
version: '1.0.0',
scripts: {
hello: 'echo "Hello World"'
}
};
await fs.writeJson(path.join(tempDir, 'package.json'), packageJson);
// This should not throw
await packageManager.runScript('hello', { silent: true });
});
test('should handle script that exits with error code', async () => {
const packageJson = {
name: 'test-package',
version: '1.0.0',
scripts: {
fail: 'nonexistentcommand123456' // This command doesn't exist
}
};
await fs.writeJson(path.join(tempDir, 'package.json'), packageJson);
await expect(packageManager.runScript('fail')).rejects.toThrow(
'Script "fail" exited with code 127'
);
});
});