Repository navigation
refactor(copilot-rule): reuse toPosixPath and switch sameRelativePath to object params #1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -796,6 +796,28 @@ description: "Test trimming" | |
| }); | ||
| }); | ||
|
|
||
| describe("forDeletion", () => { | ||
| it("should mark root deletion target when separators are mixed", () => { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small naming nit: both new cases say "separators are mixed" / "mixed separators", but neither input actually mixes |
||
| const copilotRule = CopilotRule.forDeletion({ | ||
| outputRoot: testDir, | ||
| relativeDirPath: ".github\\", | ||
| relativeFilePath: "copilot-instructions.md", | ||
| }); | ||
|
|
||
| expect(copilotRule.isRoot()).toBe(true); | ||
| }); | ||
|
|
||
| it("should treat non-root paths as non-root even with mixed separators", () => { | ||
| const copilotRule = CopilotRule.forDeletion({ | ||
| outputRoot: testDir, | ||
| relativeDirPath: ".github\\instructions", | ||
| relativeFilePath: "feature.instructions.md", | ||
| }); | ||
|
|
||
| expect(copilotRule.isRoot()).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("validate", () => { | ||
| it("should return success for valid frontmatter", () => { | ||
| const copilotRule = new CopilotRule({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import { z } from "zod/mini"; | |
| import { RULESYNC_RULES_RELATIVE_DIR_PATH } from "../../constants/rulesync-paths.js"; | ||
| import { ValidationResult } from "../../types/ai-file.js"; | ||
| import { formatError } from "../../utils/error.js"; | ||
| import { readFileContent } from "../../utils/file.js"; | ||
| import { readFileContent, toPosixPath } from "../../utils/file.js"; | ||
| import { parseFrontmatter, stringifyFrontmatter } from "../../utils/frontmatter.js"; | ||
| import { RulesyncRule, RulesyncRuleFrontmatter } from "./rulesync-rule.js"; | ||
| import { | ||
|
|
@@ -48,12 +48,18 @@ export type CopilotRuleSettablePathsGlobal = ToolRuleSettablePathsGlobal & { | |
| }; | ||
| }; | ||
|
|
||
| const normalizeRelativePath = (path: string): string => | ||
| path.replace(/\\/g, "/").replace(/\/+/g, "/"); | ||
| // toPosixPath converts backslashes to forward slashes so paths compare equally | ||
| // on Windows and POSIX. The extra slash collapse covers a WSL/mixed-separator | ||
| // edge case where `node:path/posix.join` keeps a literal backslash inside an | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: this references
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment refers to |
||
| // input segment (e.g. ".github\\") and produces ".github//instructions/x.md" | ||
| // after the backslash is rewritten. | ||
| const normalizeRelativePath = (p: string): string => toPosixPath(p).replace(/\/+/g, "/"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
|
||
| const sameRelativePath = (leftDir: string, leftFile: string, rightDir: string, rightFile: string) => | ||
| normalizeRelativePath(join(leftDir, leftFile)) === | ||
| normalizeRelativePath(join(rightDir, rightFile)); | ||
| type RelativePathParts = { dir: string; file: string }; | ||
|
|
||
| const sameRelativePath = (left: RelativePathParts, right: RelativePathParts): boolean => | ||
| normalizeRelativePath(join(left.dir, left.file)) === | ||
| normalizeRelativePath(join(right.dir, right.file)); | ||
|
|
||
| /** | ||
| * Rule generator for GitHub Copilot | ||
|
|
@@ -217,10 +223,8 @@ export class CopilotRule extends ToolRule { | |
| const paths = this.getSettablePaths({ global }); | ||
| const isRoot = relativeDirPath | ||
| ? sameRelativePath( | ||
| relativeDirPath, | ||
| relativeFilePath, | ||
| paths.root.relativeDirPath, | ||
| paths.root.relativeFilePath, | ||
| { dir: relativeDirPath, file: relativeFilePath }, | ||
| { dir: paths.root.relativeDirPath, file: paths.root.relativeFilePath }, | ||
| ) | ||
| : relativeFilePath === paths.root.relativeFilePath; | ||
| const resolvedRelativeDirPath = | ||
|
|
@@ -282,10 +286,8 @@ export class CopilotRule extends ToolRule { | |
| }: ToolRuleForDeletionParams): CopilotRule { | ||
| const paths = this.getSettablePaths({ global }); | ||
| const isRoot = sameRelativePath( | ||
| relativeDirPath, | ||
| relativeFilePath, | ||
| paths.root.relativeDirPath, | ||
| paths.root.relativeFilePath, | ||
| { dir: relativeDirPath, file: relativeFilePath }, | ||
| { dir: paths.root.relativeDirPath, file: paths.root.relativeFilePath }, | ||
| ); | ||
|
|
||
| return new CopilotRule({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice that
forDeletionnow has coverage for the backslash path. A symmetric test forfromFilewithrelativeDirPath: ".github\\instructions"would close the other half of the issue's coverage ask, sincefromFilealso routes throughsameRelativePath. Not blocking — the helper is shared — but adding it would make the safety net explicit at both call sites.