File: npath.test.ts

package info (click to toggle)
node-yarnpkg 4.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,752 kB
  • sloc: javascript: 38,953; ansic: 26,035; cpp: 7,247; sh: 2,829; makefile: 724; perl: 493
file content (215 lines) | stat: -rw-r--r-- 9,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
describe(`Portable paths`, () => {
  for (const platform of [`darwin`, `win32`]) {
    let realPlatform: string;

    describe(`Platform ${platform}`, () => {
      let npath: typeof import('../sources/path').npath;

      beforeAll(() => {
        realPlatform = process.platform;
        Object.defineProperty(process, `platform`, {
          configurable: true,
          value: platform,
        });

        jest.resetModules();
        npath = require(`../sources/path`).npath;
      });

      afterAll(() => {
        Object.defineProperty(process, `platform`, {
          configurable: true,
          value: realPlatform,
        });
      });

      describe(`toPortablePath`, () => {
        if (platform !== `win32`) {
          it(`shouldn't change paths on non-Windows platform`, () => {
            const inputPath = `C:\\Users\\user\\proj`;
            const outputPath = inputPath;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });
        } else {
          it(`shouldn't change absolute posix paths when producing portable path`, () => {
            const inputPath = `/home/user/proj`;
            const outputPath = inputPath;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`shouldn't change absolute paths that are already portable`, () => {
            const inputPath = `/c:/Users/user/proj`;
            const outputPath = `/c:/Users/user/proj`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should normalize the slashes in relative Windows paths`, () => {
            const inputPath = `..\\Users\\user/proj`;
            const outputPath = `../Users/user/proj`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform Windows paths into their posix counterparts (uppercase drive)`, () => {
            const inputPath = `C:\\Users\\user\\proj`;
            const outputPath = `/C:/Users/user/proj`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform Windows paths into their posix counterparts (lowercase drive)`, () => {
            const inputPath = `c:\\Users\\user\\proj`;
            const outputPath = `/c:/Users/user/proj`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform Windows paths into their posix counterparts (forward slashes)`, () => {
            const inputPath = `C:/Users/user/proj`;
            const outputPath = `/C:/Users/user/proj`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support Windows paths that contain both backslashes and forward slashes`, () => {
            const inputPath = `C:/Users\\user/proj`;
            const outputPath = `/C:/Users/user/proj`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support drive: Windows paths`, () => {
            const inputPath = `C:`;
            const outputPath = `/C:`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support UNC Windows paths (\\\\[server]\\[sharename]\\)`, () => {
            const inputPath = `\\\\Server01\\user\\docs\\Letter.txt`;
            const outputPath = `/unc/Server01/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support forward slash UNC Windows paths (//[server]/[sharename]/)`, () => {
            const inputPath = `//Server01/user/docs/Letter.txt`;
            const outputPath = `/unc/Server01/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support Long UNC Windows paths (\\\\?\\[server]\\[sharename]\\)`, () => {
            const inputPath = `\\\\?\\Server01\\user\\docs\\Letter.txt`;
            const outputPath = `/unc/?/Server01/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support forward slash Long UNC Windows paths (//?/[server]/[sharename]/) `, () => {
            const inputPath = `//?/Server01/user/docs/Letter.txt`;
            const outputPath = `/unc/?/Server01/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support Long UNC Windows paths (\\\\?\\UNC\\[server]\\[sharename]\\)`, () => {
            const inputPath = `\\\\?\\UNC\\Server01\\user\\docs\\Letter.txt`;
            const outputPath = `/unc/?/UNC/Server01/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support Long UNC Windows paths (\\\\?\\[drive_spec]:\\)`, () => {
            const inputPath = `\\\\?\\C:\\user\\docs\\Letter.txt`;
            const outputPath = `/unc/?/C:/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support Long UNC Windows paths with dot (\\\\.\\[physical_device]\\)`, () => {
            const inputPath = `\\\\.\\PhysicalDevice\\user\\docs\\Letter.txt`;
            const outputPath = `/unc/.dot/PhysicalDevice/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should support forward slash Long UNC Windows paths with dot (//./[physical_device]/)`, () => {
            const inputPath = `//./PhysicalDevice/user/docs/Letter.txt`;
            const outputPath = `/unc/.dot/PhysicalDevice/user/docs/Letter.txt`;
            expect(npath.toPortablePath(inputPath)).toEqual(outputPath);
          });
        }
      });

      describe(`fromPortablePath`, () => {
        if (platform !== `win32`) {
          it(`shouldn't change portable paths on non-Windows platforms`, () => {
            const inputPath = `/c:/Users/user/proj`;
            const outputPath = inputPath;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });
        } else {
          it(`shouldn't change absolute posix paths when producing native path`, () => {
            const inputPath = `/home/user/proj`;
            const outputPath = `/home/user/proj`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`shouldn't change relative posix paths when producing native paths`, () => {
            const inputPath = `../Users/user/proj`;
            const outputPath = inputPath;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`shouldn't change absolute path when it is already Windows`, () => {
            const inputPath = `c:\\Users\\user\\proj`;
            const outputPath = inputPath;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back Windows paths on Windows platforms (lowercase drive)`, () => {
            const inputPath = `/c:/Users/user/proj`;
            const outputPath = `c:\\Users\\user\\proj`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back Windows paths on Windows platforms (uppercase drive)`, () => {
            const inputPath = `/C:/Users/user/proj`;
            const outputPath = `C:\\Users\\user\\proj`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back Windows paths on Windows platforms (mixed path)`, () => {
            const inputPath = `/c:/Users\\user/proj`;
            const outputPath = `c:\\Users\\user\\proj`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back drive: on Windows platforms`, () => {
            const inputPath = `/C:`;
            const outputPath = `C:`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back UNC Windows paths (/unc/[server]/[sharename]/)`, () => {
            const inputPath = `/unc/Server01/user/docs/Letter.txt`;
            const outputPath = `\\\\Server01\\user\\docs\\Letter.txt`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back Long UNC Windows paths (/unc/?/[server]/[sharename]/)`, () => {
            const inputPath = `/unc/?/Server01/user/docs/Letter.txt`;
            const outputPath = `\\\\?\\Server01\\user\\docs\\Letter.txt`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back Long UNC Windows paths (/unc/?/UNC/[server]/[sharename]/)`, () => {
            const inputPath = `/unc/?/UNC/Server01/user/docs/Letter.txt`;
            const outputPath = `\\\\?\\UNC\\Server01\\user\\docs\\Letter.txt`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back Long UNC Windows paths (/unc/?/[drive_spec]:/)`, () => {
            const inputPath = `/unc/?/C:/user/docs/Letter.txt`;
            const outputPath = `\\\\?\\C:\\user\\docs\\Letter.txt`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });

          it(`should transform back Long UNC Windows paths with dot (/unc/.dot/[physical_device]/)`, () => {
            const inputPath = `/unc/.dot/PhysicalDevice/user/docs/Letter.txt`;
            const outputPath = `\\\\.\\PhysicalDevice\\user\\docs\\Letter.txt`;
            expect(npath.fromPortablePath(inputPath)).toEqual(outputPath);
          });
        }
      });
    });
  }
});