-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: truncate tagToString
to max filename len
#5632
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
afb6bd0
fix: truncate `tagToString` to max filename len
Patrick-Erichsen c452f90
Update utils.ts
Patrick-Erichsen 372fd87
Merge branch 'main' into pe/tagToString-fix
Patrick-Erichsen 98efbbd
fix: incorporate feedback
Patrick-Erichsen d56201c
Update unit-testing-rules.yaml
Patrick-Erichsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: unit-testing-rules | ||
version: 0.0.1 | ||
schema: v1 | ||
rules: | ||
- name: unit-testing-rules | ||
rule: >- | ||
For unit testing in this project: | ||
|
||
|
||
1. The project uses Jest as the testing framework | ||
|
||
2. Run tests using `npm test` from within the specific package/module | ||
directory | ||
|
||
3. Command structure: `cd [directory] && npm test -- [test file path]` | ||
|
||
4. The test script uses experimental VM modules via NODE_OPTIONS flag | ||
|
||
5. Test files follow the pattern `*.test.ts` | ||
|
||
6. Tests must import Jest with `import { jest } from "@jest/globals";` | ||
|
||
7. Run tests from within the specific package directory (e.g., `cd core` | ||
for core module tests) | ||
|
||
8. Write tests as top-level `test()` functions - DO NOT use `describe()` | ||
blocks | ||
|
||
9. Include the function name being tested in the test description for | ||
clarity |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { IndexTag } from ".."; | ||
import { tagToString } from "./utils"; | ||
|
||
test("tagToString returns full tag string when under length limit", () => { | ||
const tag: IndexTag = { | ||
directory: "/normal/path/to/repo", | ||
branch: "main", | ||
artifactId: "12345", | ||
}; | ||
|
||
expect(tagToString(tag)).toBe("/normal/path/to/repo::main::12345"); | ||
}); | ||
|
||
test("tagToString truncates beginning of directory when path is too long", () => { | ||
// Create a very long directory path that exceeds MAX_DIR_LENGTH (200) | ||
const longPrefix = "/very/long/path/that/will/be/truncated/"; | ||
const importantSuffix = "/user/important-project/src/feature"; | ||
const longPath = longPrefix + "x".repeat(200) + importantSuffix; | ||
|
||
const tag: IndexTag = { | ||
directory: longPath, | ||
branch: "feature-branch", | ||
artifactId: "67890", | ||
}; | ||
|
||
const result = tagToString(tag); | ||
|
||
// The result should keep the important suffix part | ||
expect(result).toContain(importantSuffix); | ||
// The result should NOT contain the beginning of the path | ||
expect(result).not.toContain(longPrefix); | ||
// The result should include the branch and artifactId | ||
expect(result).toContain("::feature-branch::67890"); | ||
// The result should be within the MAX_TABLE_NAME_LENGTH limit (240) | ||
expect(result.length).toBeLessThanOrEqual(240); | ||
}); | ||
|
||
test("tagToString preserves branch and artifactId exactly, even when truncating", () => { | ||
const longPath = "/a".repeat(300); // Much longer than MAX_DIR_LENGTH | ||
const tag: IndexTag = { | ||
directory: longPath, | ||
branch: "release-v2.0", | ||
artifactId: "build-123", | ||
}; | ||
|
||
const result = tagToString(tag); | ||
|
||
// Should contain the exact branch and artifactId | ||
expect(result).toContain("::release-v2.0::build-123"); | ||
// Should contain the end of the path | ||
expect(result).toContain("/a/a/a"); | ||
// Should not contain the full original path (it should be truncated) | ||
expect(result.length).toBeLessThan( | ||
longPath.length + "::release-v2.0::build-123".length, | ||
); | ||
// The result should be within the MAX_TABLE_NAME_LENGTH limit | ||
expect(result.length).toBeLessThanOrEqual(240); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { IndexTag } from ".."; | ||
|
||
// Maximum length for table names to stay under OS filename limits | ||
const MAX_TABLE_NAME_LENGTH = 240; | ||
Patrick-Erichsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Leave room for branch and artifactId | ||
const MAX_DIR_LENGTH = 200; | ||
|
||
/** | ||
* Converts an IndexTag to a string representation, safely handling long paths. | ||
* | ||
* The string is used as a table name and identifier in various places, so it needs | ||
* to stay under OS filename length limits (typically 255 chars). This is especially | ||
* important for dev containers where the directory path can be very long due to | ||
* containing container configuration. | ||
* | ||
* The format is: "{directory}::{branch}::{artifactId}" | ||
* | ||
* To handle long paths: | ||
* 1. First tries the full string - most backwards compatible | ||
* 2. If too long, truncates directory from the beginning to maintain uniqueness | ||
* (since final parts of paths are more unique than prefixes) | ||
* 3. Finally ensures entire string stays under MAX_TABLE_NAME_LENGTH for OS compatibility | ||
* | ||
* @param tag The tag containing directory, branch, and artifactId | ||
* @returns A string representation safe for use as a table name | ||
*/ | ||
export function tagToString(tag: IndexTag): string { | ||
Patrick-Erichsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const result = `${tag.directory}::${tag.branch}::${tag.artifactId}`; | ||
|
||
if (result.length <= MAX_TABLE_NAME_LENGTH) { | ||
return result; | ||
} | ||
|
||
// Truncate from the beginning of directory path to preserve the more unique end parts | ||
const dir = | ||
tag.directory.length > MAX_DIR_LENGTH | ||
? tag.directory.slice(tag.directory.length - MAX_DIR_LENGTH) | ||
: tag.directory; | ||
|
||
return `${dir}::${tag.branch}::${tag.artifactId}`.slice( | ||
0, | ||
Patrick-Erichsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MAX_TABLE_NAME_LENGTH, | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nothing to do in your PR here, but reminder that we should add prettier as a CI step to resolve these messy diffs