Skip to content

Commit

Permalink
Merge pull request #794 from pmcelhaney/backlash-in-context-import
Browse files Browse the repository at this point in the history
backlash in context import
  • Loading branch information
pmcelhaney committed Mar 12, 2024
2 parents 8e6f6fd + 464c406 commit 9315009
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-flowers-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fixed [#788](https://github.com/pmcelhaney/counterfact/issues/788): On Windows, the import field from path-types to context gets wrong slashes while others are fine
10 changes: 6 additions & 4 deletions src/typescript-generator/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ export class Context {
}

findContextPath(destination, path) {
return nodePath.relative(
nodePath.join(destination, nodePath.dirname(path)),
this.nearestContextFile(destination, path),
);
return nodePath
.relative(
nodePath.join(destination, nodePath.dirname(path)),
this.nearestContextFile(destination, path),
)
.replaceAll("\\", "/");
}

nearestContextFile(destination, path) {
Expand Down
22 changes: 6 additions & 16 deletions test/typescript-generator/repository.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { sep } from "node:path";

import { usingTemporaryFiles } from "using-temporary-files";

import { Repository } from "../../src/typescript-generator/repository.js";

function osPaths(testCases) {
return testCases.map((testCase) =>
testCase.map((path) => path.replaceAll("/", sep)),
);
}

describe("a Repository", () => {
it("creates a new Script or returns an existing one", () => {
const repository = new Repository("/base/path");
Expand All @@ -22,14 +14,12 @@ describe("a Repository", () => {
expect(a2).toBe(a);
});

it.each(
osPaths([
["./path-types/x.ts", "../paths/_.context.ts"],
["./path-types/a/x.ts", "../../paths/_.context.ts"],
["./path-types/a/b/x.ts", "../../../paths/a/b/_.context.ts"],
["./path-types/a/b/c/x.ts", "../../../../paths/a/b/_.context.ts"],
]),
)(
it.each([
["./path-types/x.ts", "../paths/_.context.ts"],
["./path-types/a/x.ts", "../../paths/_.context.ts"],
["./path-types/a/b/x.ts", "../../../paths/a/b/_.context.ts"],
["./path-types/a/b/c/x.ts", "../../../../paths/a/b/_.context.ts"],
])(
"finds the relative location of the most relevant _.context.ts file (%s => %s)",
async (importingFilePath, relativePathToNearestContext) => {
await usingTemporaryFiles(async ({ add, path }) => {
Expand Down

0 comments on commit 9315009

Please sign in to comment.