-
Notifications
You must be signed in to change notification settings - Fork 430
feat(@huggingface/hub): adding scanCacheDir
#908
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
coyotte508
merged 15 commits into
huggingface:main
from
axel7083:feature/support-scan-cache
Sep 26, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4eb3c85
feat(hub): support scan cache
axel7083 8e4e46e
fix: formatting and linter
axel7083 e003bd6
fix: tests
axel7083 629e8c5
fix: naming convention
axel7083 fa170b9
fix: adding cache-management to index.ts
axel7083 fde3dee
fix(browser): exclude cache management
axel7083 7a53407
revert: useless changes
axel7083 77f2198
fix: avoid homedir call on import
axel7083 53c81ff
fix: apply @coyotte508 code suggestion
axel7083 0f33684
fix: blob property type
axel7083 25b4c8a
fix: missing @coyotte508 comment
axel7083 f264a81
fix: refactor CachedFileInfo
axel7083 76a8ec7
Apply suggestions from @Wauplin
axel7083 2080c0d
fix: unit tests
axel7083 70afe80
Merge branch 'main' into feature/support-scan-cache
Wauplin 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
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,137 @@ | ||
import { describe, test, expect, vi, beforeEach } from "vitest"; | ||
import { | ||
scanCacheDir, | ||
scanCachedRepo, | ||
scanSnapshotDir, | ||
parseRepoType, | ||
getBlobStat, | ||
type CachedFileInfo, | ||
} from "./cache-management"; | ||
import { stat, readdir, realpath, lstat } from "node:fs/promises"; | ||
import type { Dirent, Stats } from "node:fs"; | ||
import { join } from "node:path"; | ||
|
||
// Mocks | ||
vi.mock("node:fs/promises"); | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
describe("scanCacheDir", () => { | ||
test("should throw an error if cacheDir is not a directory", async () => { | ||
vi.mocked(stat).mockResolvedValueOnce({ | ||
isDirectory: () => false, | ||
} as Stats); | ||
|
||
await expect(scanCacheDir("/fake/dir")).rejects.toThrow("Scan cache expects a directory"); | ||
}); | ||
|
||
test("empty directory should return an empty set of repository and no warnings", async () => { | ||
vi.mocked(stat).mockResolvedValueOnce({ | ||
isDirectory: () => true, | ||
} as Stats); | ||
|
||
// mock empty cache folder | ||
vi.mocked(readdir).mockResolvedValue([]); | ||
|
||
const result = await scanCacheDir("/fake/dir"); | ||
|
||
// cacheDir must have been read | ||
expect(readdir).toHaveBeenCalledWith("/fake/dir"); | ||
|
||
expect(result.warnings.length).toBe(0); | ||
expect(result.repos).toHaveLength(0); | ||
expect(result.size).toBe(0); | ||
}); | ||
}); | ||
|
||
describe("scanCachedRepo", () => { | ||
test("should throw an error for invalid repo path", async () => { | ||
await expect(() => { | ||
return scanCachedRepo("/fake/repo_path"); | ||
}).rejects.toThrow("Repo path is not a valid HuggingFace cache directory"); | ||
}); | ||
|
||
test("should throw an error if the snapshot folder does not exist", async () => { | ||
vi.mocked(readdir).mockResolvedValue([]); | ||
vi.mocked(stat).mockResolvedValue({ | ||
isDirectory: () => false, | ||
} as Stats); | ||
|
||
await expect(() => { | ||
return scanCachedRepo("/fake/cacheDir/models--hello-world--name"); | ||
}).rejects.toThrow("Snapshots dir doesn't exist in cached repo"); | ||
}); | ||
|
||
test("should properly parse the repository name", async () => { | ||
const repoPath = "/fake/cacheDir/models--hello-world--name"; | ||
vi.mocked(readdir).mockResolvedValue([]); | ||
vi.mocked(stat).mockResolvedValue({ | ||
isDirectory: () => true, | ||
} as Stats); | ||
|
||
const result = await scanCachedRepo(repoPath); | ||
expect(readdir).toHaveBeenCalledWith(join(repoPath, "refs"), { | ||
withFileTypes: true, | ||
}); | ||
|
||
expect(result.id.name).toBe("hello-world/name"); | ||
expect(result.id.type).toBe("model"); | ||
}); | ||
}); | ||
|
||
describe("scanSnapshotDir", () => { | ||
test("should scan a valid snapshot directory", async () => { | ||
const cachedFiles: CachedFileInfo[] = []; | ||
const blobStats = new Map<string, Stats>(); | ||
vi.mocked(readdir).mockResolvedValueOnce([{ name: "file1", isDirectory: () => false } as Dirent]); | ||
|
||
vi.mocked(realpath).mockResolvedValueOnce("/fake/realpath"); | ||
vi.mocked(lstat).mockResolvedValueOnce({ size: 1024, atimeMs: Date.now(), mtimeMs: Date.now() } as Stats); | ||
|
||
await scanSnapshotDir("/fake/revision", cachedFiles, blobStats); | ||
|
||
expect(cachedFiles).toHaveLength(1); | ||
expect(blobStats.size).toBe(1); | ||
}); | ||
}); | ||
|
||
describe("getBlobStat", () => { | ||
test("should retrieve blob stat if already cached", async () => { | ||
const blobStats = new Map<string, Stats>([["/fake/blob", { size: 1024 } as Stats]]); | ||
const result = await getBlobStat("/fake/blob", blobStats); | ||
|
||
expect(lstat).not.toHaveBeenCalled(); | ||
expect(result.size).toBe(1024); | ||
}); | ||
|
||
test("should fetch and cache blob stat if not cached", async () => { | ||
const blobStats = new Map(); | ||
vi.mocked(lstat).mockResolvedValueOnce({ size: 2048 } as Stats); | ||
|
||
const result = await getBlobStat("/fake/blob", blobStats); | ||
|
||
expect(result.size).toBe(2048); | ||
expect(blobStats.size).toBe(1); | ||
}); | ||
}); | ||
|
||
describe("parseRepoType", () => { | ||
test("should parse models repo type", () => { | ||
expect(parseRepoType("models")).toBe("model"); | ||
}); | ||
|
||
test("should parse dataset repo type", () => { | ||
expect(parseRepoType("datasets")).toBe("dataset"); | ||
}); | ||
|
||
test("should parse space repo type", () => { | ||
expect(parseRepoType("spaces")).toBe("space"); | ||
}); | ||
|
||
test("should throw an error for invalid repo type", () => { | ||
expect(() => parseRepoType("invalid")).toThrowError("Invalid repo type: invalid"); | ||
}); | ||
}); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.