|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { getAsyncProcessRunnerConfig } from '@code-pushup/test-utils'; |
3 | | -import { type ProcessObserver, executeProcess } from './execute-process.js'; |
| 3 | +import { |
| 4 | + type ProcessObserver, |
| 5 | + buildCommandString, |
| 6 | + executeProcess, |
| 7 | +} from './execute-process.js'; |
4 | 8 |
|
5 | 9 | describe('executeProcess', () => { |
6 | 10 | const spyObserver: ProcessObserver = { |
@@ -90,3 +94,43 @@ describe('executeProcess', () => { |
90 | 94 | expect(spyObserver.onComplete).toHaveBeenCalledOnce(); |
91 | 95 | }); |
92 | 96 | }); |
| 97 | + |
| 98 | +describe('buildCommandString', () => { |
| 99 | + it('should return command when no args provided', () => { |
| 100 | + expect(buildCommandString('node')).toBe('node'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return command when empty args array provided', () => { |
| 104 | + expect(buildCommandString('node', [])).toBe('node'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return command with simple args', () => { |
| 108 | + expect(buildCommandString('npm', ['install', 'package'])).toBe( |
| 109 | + 'npm install package', |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should escape args with spaces', () => { |
| 114 | + expect(buildCommandString('echo', ['hello world'])).toBe( |
| 115 | + 'echo "hello world"', |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should escape args with double quotes for shell safety and cross-platform compatibility', () => { |
| 120 | + expect(buildCommandString('echo', ['say "hello"'])).toBe( |
| 121 | + 'echo "say \\"hello\\""', |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should escape args with single quotes for shell safety and cross-platform compatibility', () => { |
| 126 | + expect(buildCommandString('echo', ["it's working"])).toBe( |
| 127 | + 'echo "it\'s working"', |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should handle mixed escaped and non-escaped args', () => { |
| 132 | + expect( |
| 133 | + buildCommandString('node', ['script.js', 'hello world', '--flag']), |
| 134 | + ).toBe('node script.js "hello world" --flag'); |
| 135 | + }); |
| 136 | +}); |
0 commit comments