@@ -2,6 +2,8 @@ import { describe, expect, it } from "vitest";
2
2
import fixturez from "fixturez" ;
3
3
import stripAnsi from "strip-ansi" ;
4
4
import { exec } from "tinyexec" ;
5
+ import fs from "node:fs" ;
6
+ import path from "node:path" ;
5
7
6
8
const f = fixturez ( __dirname ) ;
7
9
@@ -28,18 +30,11 @@ describe("Run command", () => {
28
30
] ) (
29
31
'should execute "%s %s" and exit with %i' ,
30
32
async ( arg0 , arg1 , expectedExitCode ) => {
31
- const { exitCode, stdout, stderr } = await exec (
32
- "node" ,
33
- [ require . resolve ( "../bin.js" ) , "run" , arg0 , arg1 ] ,
34
- {
35
- nodeOptions : {
36
- cwd : f . find ( "basic-with-scripts" ) ,
37
- env : {
38
- ...process . env ,
39
- NODE_OPTIONS : "--experimental-strip-types" ,
40
- } ,
41
- } ,
42
- }
33
+ const { exitCode, stdout, stderr } = await executeBin (
34
+ f . find ( "basic-with-scripts" ) ,
35
+ "run" ,
36
+ arg0 ,
37
+ arg1
43
38
) ;
44
39
expect ( exitCode ) . toBe ( expectedExitCode ) ;
45
40
expect ( stripAnsi ( stdout . toString ( ) ) ) . toMatchSnapshot ( "stdout" ) ;
@@ -49,3 +44,64 @@ describe("Run command", () => {
49
44
}
50
45
) ;
51
46
} ) ;
47
+
48
+ describe ( "Fix command" , ( ) => {
49
+ it . each ( [
50
+ [ "package-one" , "fix" , "lf" , 0 ] ,
51
+ [ "package-one" , "fix" , "crlf" , 0 ] ,
52
+ ] as const satisfies [ string , string , LineEndings , number ] [ ] ) (
53
+ 'should execute "%s %s" without changing line endings from %s' ,
54
+ async ( arg0 , arg1 , sourceLineEnding , expectedExitCode ) => {
55
+ // arrange
56
+ const temp = f . copy ( "basic-with-scripts" ) ;
57
+ const filePath = path . join ( temp , "package.json" ) ;
58
+ convertFileLineEndings ( filePath , sourceLineEnding ) ;
59
+ // act
60
+ const { exitCode } = await executeBin ( temp , "fix" , arg0 , arg1 ) ;
61
+ // assert
62
+ expect ( exitCode ) . toBe ( expectedExitCode ) ;
63
+ const fixedPackageFile = fs . readFileSync ( filePath , "utf8" ) ;
64
+ f . cleanup ( ) ;
65
+ expect ( detectLineEndings ( fixedPackageFile ) ) . toBe ( sourceLineEnding ) ;
66
+ }
67
+ ) ;
68
+ } ) ;
69
+
70
+ type LineEndings = "crlf" | "lf" ;
71
+
72
+ function convertFileLineEndings ( path : string , targetLineEnding : LineEndings ) {
73
+ let file = fs . readFileSync ( path , "utf8" ) ;
74
+ // detect mixed line endings
75
+ if (
76
+ file . includes ( "\r\n" ) &&
77
+ ( file . match ( / \r \n / g) || [ ] ) . length !== ( file . match ( / \n / g) || [ ] ) . length
78
+ ) {
79
+ throw new Error ( "mixed line endings in fixture file: " + path ) ;
80
+ }
81
+ // if the line endings match, we don't need to convert the file
82
+ if ( file . includes ( "\r\n" ) === ( targetLineEnding === "crlf" ) ) {
83
+ return ;
84
+ }
85
+ const sourceLineEndingText = targetLineEnding === "crlf" ? "\n" : "\r\n" ;
86
+ const targetLineEndingText = targetLineEnding === "crlf" ? "\r\n" : "\r\n" ;
87
+ fs . writeFileSync (
88
+ path ,
89
+ file . replaceAll ( sourceLineEndingText , targetLineEndingText )
90
+ ) ;
91
+ }
92
+
93
+ function executeBin ( path : string , command : string , ...args : string [ ] ) {
94
+ return exec ( "node" , [ require . resolve ( "../bin.js" ) , command , ...args ] , {
95
+ nodeOptions : {
96
+ cwd : path ,
97
+ env : {
98
+ ...process . env ,
99
+ NODE_OPTIONS : "--experimental-strip-types" ,
100
+ } ,
101
+ } ,
102
+ } ) ;
103
+ }
104
+
105
+ function detectLineEndings ( content : string ) {
106
+ return ( content . includes ( "\r\n" ) ? "crlf" : "lf" ) satisfies LineEndings ;
107
+ }
0 commit comments