|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, expect, test } from 'rstack/test'; |
| 5 | +import { RSTACK_BIN_PATH } from '#test-helpers'; |
| 6 | + |
| 7 | +let projectPath: string; |
| 8 | + |
| 9 | +const writeProjectFile = (filePath: string, content: string): string => { |
| 10 | + const absolutePath = path.join(projectPath, filePath); |
| 11 | + mkdirSync(path.dirname(absolutePath), { recursive: true }); |
| 12 | + writeFileSync(absolutePath, content); |
| 13 | + return absolutePath; |
| 14 | +}; |
| 15 | + |
| 16 | +const readProjectFile = (filePath: string): string => |
| 17 | + readFileSync(path.join(projectPath, filePath), 'utf8'); |
| 18 | + |
| 19 | +const runCLI = (args: string[]) => { |
| 20 | + const env: NodeJS.ProcessEnv = { ...process.env, NO_COLOR: '1' }; |
| 21 | + delete env.FORCE_COLOR; |
| 22 | + |
| 23 | + return spawnSync(process.execPath, [RSTACK_BIN_PATH, ...args], { |
| 24 | + cwd: projectPath, |
| 25 | + encoding: 'utf8', |
| 26 | + env, |
| 27 | + }); |
| 28 | +}; |
| 29 | + |
| 30 | +const runFmt = (args: string[] = []) => runCLI(['fmt', ...args]); |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + projectPath = mkdtempSync(path.join(import.meta.dirname, 'fmt-project-')); |
| 34 | + writeProjectFile('rstack.config.ts', 'export {};\n'); |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + rmSync(projectPath, { force: true, recursive: true }); |
| 39 | +}); |
| 40 | + |
| 41 | +test('displays fmt help without loading config', () => { |
| 42 | + writeProjectFile('rstack.config.ts', 'throw new Error("must not load");\n'); |
| 43 | + |
| 44 | + const topLevel = runCLI(['--help']); |
| 45 | + const fmt = runFmt(['--help']); |
| 46 | + |
| 47 | + expect(topLevel.status).toBe(0); |
| 48 | + expect(topLevel.stdout).toContain('fmt Format code'); |
| 49 | + expect(fmt.status).toBe(0); |
| 50 | + expect(fmt.stdout).toContain('Usage:\n $ rs fmt [options] [files/globs...]'); |
| 51 | + expect(fmt.stderr).toBe(''); |
| 52 | +}); |
| 53 | + |
| 54 | +test('returns exit code 1 for invalid arguments', () => { |
| 55 | + const result = runFmt(['--write', '--check']); |
| 56 | + |
| 57 | + expect(result.status).toBe(1); |
| 58 | + expect(result.stdout).toBe(''); |
| 59 | + expect(result.stderr).toContain( |
| 60 | + 'The --write, --check, and --list-different options cannot be used together.', |
| 61 | + ); |
| 62 | +}); |
| 63 | + |
| 64 | +test('formats the current directory with Prettier defaults', () => { |
| 65 | + writeProjectFile('index.ts', 'const message="hello"'); |
| 66 | + |
| 67 | + const result = runFmt(); |
| 68 | + |
| 69 | + expect(result.status).toBe(0); |
| 70 | + expect(result.stdout).toBe('index.ts\n'); |
| 71 | + expect(result.stderr).toBe(''); |
| 72 | + expect(readProjectFile('index.ts')).toBe('const message = "hello";\n'); |
| 73 | +}); |
| 74 | + |
| 75 | +test('does not load Prettier config or ignore files', () => { |
| 76 | + writeProjectFile('.prettierrc.json', '{ "singleQuote": true, "semi": false }\n'); |
| 77 | + writeProjectFile('.prettierignore', 'index.ts\n'); |
| 78 | + writeProjectFile('.editorconfig', 'root = true\n\n[*]\nindent_style = space\nindent_size = 8\n'); |
| 79 | + writeProjectFile('index.ts', "function getMessage(){\n return 'hello'\n}"); |
| 80 | + |
| 81 | + const result = runFmt(['index.ts']); |
| 82 | + |
| 83 | + expect(result.status).toBe(0); |
| 84 | + expect(result.stdout).toBe('index.ts\n'); |
| 85 | + expect(result.stderr).toBe(''); |
| 86 | + expect(readProjectFile('index.ts')).toBe('function getMessage() {\n return "hello";\n}\n'); |
| 87 | +}); |
| 88 | + |
| 89 | +test('applies define.fmt options, overrides, ignore patterns, and globs', () => { |
| 90 | + writeProjectFile( |
| 91 | + 'rstack.config.ts', |
| 92 | + `import { define } from 'rstack'; |
| 93 | +
|
| 94 | +define.fmt({ |
| 95 | + singleQuote: true, |
| 96 | + ignorePatterns: ['src/ignored.ts'], |
| 97 | + overrides: [ |
| 98 | + { |
| 99 | + files: '*.test.ts', |
| 100 | + options: { |
| 101 | + semi: false, |
| 102 | + }, |
| 103 | + }, |
| 104 | + ], |
| 105 | +}); |
| 106 | +`, |
| 107 | + ); |
| 108 | + writeProjectFile('src/index.ts', 'const message="hello"'); |
| 109 | + writeProjectFile('src/index.test.ts', 'const test="test"'); |
| 110 | + writeProjectFile('src/ignored.ts', 'const ignored="ignored"'); |
| 111 | + writeProjectFile('src/index.js', 'const javascript="untouched"'); |
| 112 | + |
| 113 | + const result = runFmt(['--write', 'src/**/*.ts']); |
| 114 | + |
| 115 | + expect(result.status).toBe(0); |
| 116 | + expect(result.stdout).toBe('src/index.test.ts\nsrc/index.ts\n'); |
| 117 | + expect(result.stderr).toBe(''); |
| 118 | + expect(readProjectFile('src/index.ts')).toBe("const message = 'hello';\n"); |
| 119 | + expect(readProjectFile('src/index.test.ts')).toBe("const test = 'test'\n"); |
| 120 | + expect(readProjectFile('src/ignored.ts')).toBe('const ignored="ignored"'); |
| 121 | + expect(readProjectFile('src/index.js')).toBe('const javascript="untouched"'); |
| 122 | +}); |
| 123 | + |
| 124 | +test('uses an explicit Rstack config', () => { |
| 125 | + writeProjectFile( |
| 126 | + 'custom.config.ts', |
| 127 | + `import { define } from 'rstack'; |
| 128 | +
|
| 129 | +define.fmt({ |
| 130 | + singleQuote: true, |
| 131 | +}); |
| 132 | +`, |
| 133 | + ); |
| 134 | + writeProjectFile('index.ts', 'const message="hello"'); |
| 135 | + |
| 136 | + const result = runFmt(['index.ts', '--config', 'custom.config.ts']); |
| 137 | + |
| 138 | + expect(result.status).toBe(0); |
| 139 | + expect(result.stdout).toBe('index.ts\n'); |
| 140 | + expect(result.stderr).toBe(''); |
| 141 | + expect(readProjectFile('index.ts')).toBe("const message = 'hello';\n"); |
| 142 | +}); |
| 143 | + |
| 144 | +test('checks formatting without writing files', () => { |
| 145 | + const source = 'const message="hello"'; |
| 146 | + writeProjectFile('index.ts', source); |
| 147 | + |
| 148 | + const result = runFmt(['--check', 'index.ts']); |
| 149 | + |
| 150 | + expect(result.status).toBe(1); |
| 151 | + expect(result.stdout).toBe('Checking formatting...\n'); |
| 152 | + expect(result.stderr).toContain('warn index.ts'); |
| 153 | + expect(result.stderr).toContain( |
| 154 | + 'warn Code style issues found in 1 file. Run rs fmt --write to fix.', |
| 155 | + ); |
| 156 | + expect(readProjectFile('index.ts')).toBe(source); |
| 157 | + |
| 158 | + writeProjectFile('index.ts', 'const message = "hello";\n'); |
| 159 | + const formattedResult = runFmt(['--check', 'index.ts']); |
| 160 | + |
| 161 | + expect(formattedResult.status).toBe(0); |
| 162 | + expect(formattedResult.stdout).toBe( |
| 163 | + 'Checking formatting...\nAll matched files use Prettier code style!\n', |
| 164 | + ); |
| 165 | + expect(formattedResult.stderr).toBe(''); |
| 166 | +}); |
| 167 | + |
| 168 | +test('lists only paths that differ', () => { |
| 169 | + const source = 'const message="hello"'; |
| 170 | + writeProjectFile('src/index.ts', source); |
| 171 | + writeProjectFile('src/formatted.ts', 'const formatted = true;\n'); |
| 172 | + |
| 173 | + const result = runFmt(['--list-different', 'src/*.ts']); |
| 174 | + |
| 175 | + expect(result.status).toBe(1); |
| 176 | + expect(result.stdout).toBe('src/index.ts\n'); |
| 177 | + expect(result.stderr).toBe(''); |
| 178 | + expect(readProjectFile('src/index.ts')).toBe(source); |
| 179 | +}); |
| 180 | + |
| 181 | +test('returns exit code 2 for config errors', () => { |
| 182 | + writeProjectFile('rstack.config.ts', 'throw new Error("invalid fmt config");\n'); |
| 183 | + |
| 184 | + const result = runFmt(['index.ts']); |
| 185 | + |
| 186 | + expect(result.status).toBe(2); |
| 187 | + expect(result.stdout).toBe(''); |
| 188 | + expect(result.stderr).toContain('invalid fmt config'); |
| 189 | +}); |
| 190 | + |
| 191 | +test('returns exit code 2 for unsupported plugins', () => { |
| 192 | + writeProjectFile( |
| 193 | + 'rstack.config.ts', |
| 194 | + `import { define } from 'rstack'; |
| 195 | +
|
| 196 | +define.fmt({ |
| 197 | + plugins: ['prettier-plugin-example'], |
| 198 | +}); |
| 199 | +`, |
| 200 | + ); |
| 201 | + writeProjectFile('index.ts', 'const message="hello"'); |
| 202 | + |
| 203 | + const result = runFmt(['index.ts']); |
| 204 | + |
| 205 | + expect(result.status).toBe(2); |
| 206 | + expect(result.stdout).toBe(''); |
| 207 | + expect(result.stderr).toContain('Prettier plugins are not supported yet.'); |
| 208 | +}); |
| 209 | + |
| 210 | +test('returns exit code 2 for formatting errors', () => { |
| 211 | + writeProjectFile('index.ts', 'const value = ;'); |
| 212 | + |
| 213 | + const result = runFmt(['index.ts']); |
| 214 | + |
| 215 | + expect(result.status).toBe(2); |
| 216 | + expect(result.stdout).toBe(''); |
| 217 | + expect(result.stderr).toContain('error index.ts: SyntaxError: Expression expected.'); |
| 218 | +}); |
| 219 | + |
| 220 | +test('succeeds when no files can be formatted', () => { |
| 221 | + const result = runFmt(['missing/**/*.ts']); |
| 222 | + |
| 223 | + expect(result.status).toBe(0); |
| 224 | + expect(result.stdout).toBe(''); |
| 225 | + expect(result.stderr).toBe(''); |
| 226 | +}); |
0 commit comments