|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +// eslint's jest plugin static-analyzes `describe` calls and doesn't recognize |
| 4 | +// `describeWin` below as one — disable `require-top-level-describe` for the |
| 5 | +// whole file rather than scatter per-test ignore comments. |
| 6 | +/* eslint-disable jest/require-top-level-describe, jsdoc/reject-any-type */ |
| 7 | + |
| 8 | +const fs = require("fs"); |
| 9 | +const path = require("path"); |
| 10 | +const { ResolverFactory } = require("../"); |
| 11 | + |
| 12 | +// DOS device paths (`\\?\…`, `\\.\…`) are Windows-only constructs. The real |
| 13 | +// resolver tests below hit the actual filesystem through those prefixes, so |
| 14 | +// they only make sense on a Windows host. `describe.skip` elsewhere keeps CI |
| 15 | +// on other platforms green while still validating the pure logic via |
| 16 | +// `path.test.js` and `identifier.test.js`. |
| 17 | +const describeWin = process.platform === "win32" ? describe : describe.skip; |
| 18 | + |
| 19 | +describeWin("DOS device path resolution (Windows)", () => { |
| 20 | + // `path.resolve` gives us the real Windows-absolute form of the fixtures |
| 21 | + // directory (e.g. `C:\…\test\fixtures`), which we then re-address through |
| 22 | + // the Win32 file (`\\?\`) and device (`\\.\`) namespaces. |
| 23 | + const fixtures = path.resolve(__dirname, "fixtures"); |
| 24 | + const dosFixtures = `\\\\?\\${fixtures}`; |
| 25 | + const dotFixtures = `\\\\.\\${fixtures}`; |
| 26 | + |
| 27 | + // `symlinks: false` keeps the result verbatim — the realpath plugin would |
| 28 | + // otherwise strip the DOS prefix on Windows, masking what we want to test. |
| 29 | + // Default (async) filesystem calls are used — sync fs is deliberately |
| 30 | + // avoided so this exercises the same code paths as production resolves. |
| 31 | + const resolver = ResolverFactory.createResolver({ |
| 32 | + fileSystem: fs, |
| 33 | + extensions: [".js"], |
| 34 | + symlinks: false, |
| 35 | + }); |
| 36 | + |
| 37 | + test("resolves a relative request against a \\\\?\\ context", async () => { |
| 38 | + await expect(resolver.resolvePromise({}, dosFixtures, "./a")).resolves.toBe( |
| 39 | + path.join(dosFixtures, "a.js"), |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + test("resolves a relative request to a subdirectory's index.js", async () => { |
| 44 | + await expect( |
| 45 | + resolver.resolvePromise({}, dosFixtures, "./foo"), |
| 46 | + ).resolves.toBe(path.join(dosFixtures, "foo", "index.js")); |
| 47 | + }); |
| 48 | + |
| 49 | + test("resolves '.' to index.js in a \\\\?\\ context", async () => { |
| 50 | + await expect( |
| 51 | + resolver.resolvePromise({}, path.join(dosFixtures, "foo"), "."), |
| 52 | + ).resolves.toBe(path.join(dosFixtures, "foo", "index.js")); |
| 53 | + }); |
| 54 | + |
| 55 | + test("resolves an absolute \\\\?\\ request regardless of context", async () => { |
| 56 | + const request = path.join(dosFixtures, "a"); |
| 57 | + await expect(resolver.resolvePromise({}, fixtures, request)).resolves.toBe( |
| 58 | + path.join(dosFixtures, "a.js"), |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test("resolves through the \\\\.\\ device namespace", async () => { |
| 63 | + // The `\\.\` walk used to infinite-loop in `cdUp` once it reached |
| 64 | + // the bare `\` root — this test proves it terminates. |
| 65 | + await expect(resolver.resolvePromise({}, dotFixtures, "./a")).resolves.toBe( |
| 66 | + path.join(dotFixtures, "a.js"), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + test("preserves a query string on a \\\\?\\ request", async () => { |
| 71 | + // The literal `?` inside `\\?\` must not be mistaken for a query |
| 72 | + // separator — the real query is the one trailing the path. |
| 73 | + const request = `${path.join(dosFixtures, "a")}?foo=bar`; |
| 74 | + await expect(resolver.resolvePromise({}, fixtures, request)).resolves.toBe( |
| 75 | + `${path.join(dosFixtures, "a.js")}?foo=bar`, |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test("preserves a fragment on a \\\\?\\ request", async () => { |
| 80 | + const request = `${path.join(dosFixtures, "a")}#frag`; |
| 81 | + await expect(resolver.resolvePromise({}, fixtures, request)).resolves.toBe( |
| 82 | + `${path.join(dosFixtures, "a.js")}#frag`, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test("rejects a missing file under a DOS device context", async () => { |
| 87 | + await expect( |
| 88 | + resolver.resolvePromise({}, dosFixtures, "./does-not-exist"), |
| 89 | + ).rejects.toThrow(/Can't resolve/); |
| 90 | + }); |
| 91 | + |
| 92 | + test("locates the nearest package.json when resolving through \\\\?\\", (done) => { |
| 93 | + // Uses the callback form because the `request` (third callback arg) |
| 94 | + // carries `descriptionFilePath`, which the promise form drops. |
| 95 | + resolver.resolve( |
| 96 | + {}, |
| 97 | + path.join(dosFixtures, "foo"), |
| 98 | + ".", |
| 99 | + {}, |
| 100 | + (err, result, request) => { |
| 101 | + if (err) return done(err); |
| 102 | + expect(result).toBe(path.join(dosFixtures, "foo", "index.js")); |
| 103 | + // The description-file walk must terminate — if `cdUp` didn't |
| 104 | + // treat bare `\` as a root, this callback would never fire. |
| 105 | + expect(/** @type {any} */ (request).descriptionFilePath).toBe( |
| 106 | + path.join(dosFixtures, "foo", "package.json"), |
| 107 | + ); |
| 108 | + done(); |
| 109 | + }, |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
0 commit comments