-
Notifications
You must be signed in to change notification settings - Fork 530
feat(hub): adding snapshot download method #1038
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 8 commits into
huggingface:main
from
axel7083:feature/snapshot-dowload-py-compliant
Nov 19, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6613966
feat(hub): adding snapshot download method
axel7083 b63f16f
fix: params propagation
axel7083 b452eb2
docs: adding code documentation to snapshotDownload
axel7083 df29083
Update packages/hub/src/lib/snapshot-download.ts
coyotte508 5aec3d5
fix: tests
axel7083 92f453b
docs: adding cache related function to readme
axel7083 58389eb
docs: typo
axel7083 ab7f732
Update packages/hub/README.md
coyotte508 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
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,275 @@ | ||
import { expect, test, describe, vi, beforeEach } from "vitest"; | ||
import { dirname, join } from "node:path"; | ||
import { mkdir, writeFile } from "node:fs/promises"; | ||
import { getHFHubCachePath } from "./cache-management"; | ||
import { downloadFileToCacheDir } from "./download-file-to-cache-dir"; | ||
import { snapshotDownload } from "./snapshot-download"; | ||
import type { ListFileEntry } from "./list-files"; | ||
import { listFiles } from "./list-files"; | ||
import { modelInfo } from "./model-info"; | ||
import type { ModelEntry } from "./list-models"; | ||
import type { ApiModelInfo } from "../types/api/api-model"; | ||
import { datasetInfo } from "./dataset-info"; | ||
import type { DatasetEntry } from "./list-datasets"; | ||
import type { ApiDatasetInfo } from "../types/api/api-dataset"; | ||
import { spaceInfo } from "./space-info"; | ||
import type { SpaceEntry } from "./list-spaces"; | ||
import type { ApiSpaceInfo } from "../types/api/api-space"; | ||
|
||
vi.mock("node:fs/promises", () => ({ | ||
writeFile: vi.fn(), | ||
mkdir: vi.fn(), | ||
})); | ||
|
||
vi.mock("./space-info", () => ({ | ||
spaceInfo: vi.fn(), | ||
})); | ||
|
||
vi.mock("./dataset-info", () => ({ | ||
datasetInfo: vi.fn(), | ||
})); | ||
|
||
vi.mock("./model-info", () => ({ | ||
modelInfo: vi.fn(), | ||
})); | ||
|
||
vi.mock("./list-files", () => ({ | ||
listFiles: vi.fn(), | ||
})); | ||
|
||
vi.mock("./download-file-to-cache-dir", () => ({ | ||
downloadFileToCacheDir: vi.fn(), | ||
})); | ||
|
||
const DUMMY_SHA = "dummy-sha"; | ||
|
||
// utility method to transform an array of ListFileEntry to an AsyncGenerator<ListFileEntry> | ||
async function* toAsyncGenerator(content: ListFileEntry[]): AsyncGenerator<ListFileEntry> { | ||
for (const entry of content) { | ||
yield Promise.resolve(entry); | ||
} | ||
} | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
vi.mocked(listFiles).mockReturnValue(toAsyncGenerator([])); | ||
|
||
// mock repo info | ||
vi.mocked(modelInfo).mockResolvedValue({ | ||
sha: DUMMY_SHA, | ||
} as ModelEntry & ApiModelInfo); | ||
vi.mocked(datasetInfo).mockResolvedValue({ | ||
sha: DUMMY_SHA, | ||
} as DatasetEntry & ApiDatasetInfo); | ||
vi.mocked(spaceInfo).mockResolvedValue({ | ||
sha: DUMMY_SHA, | ||
} as SpaceEntry & ApiSpaceInfo); | ||
}); | ||
|
||
describe("snapshotDownload", () => { | ||
test("empty AsyncGenerator should not call downloadFileToCacheDir", async () => { | ||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
}); | ||
|
||
expect(downloadFileToCacheDir).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("repo type model should use modelInfo", async () => { | ||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "model", | ||
}, | ||
}); | ||
expect(modelInfo).toHaveBeenCalledOnce(); | ||
expect(modelInfo).toHaveBeenCalledWith({ | ||
name: "foo/bar", | ||
additionalFields: ["sha"], | ||
revision: "main", | ||
repo: { | ||
name: "foo/bar", | ||
type: "model", | ||
}, | ||
}); | ||
}); | ||
|
||
test("repo type dataset should use datasetInfo", async () => { | ||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "dataset", | ||
}, | ||
}); | ||
expect(datasetInfo).toHaveBeenCalledOnce(); | ||
expect(datasetInfo).toHaveBeenCalledWith({ | ||
name: "foo/bar", | ||
additionalFields: ["sha"], | ||
revision: "main", | ||
repo: { | ||
name: "foo/bar", | ||
type: "dataset", | ||
}, | ||
}); | ||
}); | ||
|
||
test("repo type space should use spaceInfo", async () => { | ||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
}); | ||
expect(spaceInfo).toHaveBeenCalledOnce(); | ||
expect(spaceInfo).toHaveBeenCalledWith({ | ||
name: "foo/bar", | ||
additionalFields: ["sha"], | ||
revision: "main", | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
}); | ||
}); | ||
|
||
test("commitHash should be saved to ref folder", async () => { | ||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
revision: "dummy-revision", | ||
}); | ||
|
||
// cross-platform testing | ||
const expectedPath = join(getHFHubCachePath(), "spaces--foo--bar", "refs", "dummy-revision"); | ||
expect(mkdir).toHaveBeenCalledWith(dirname(expectedPath), { recursive: true }); | ||
expect(writeFile).toHaveBeenCalledWith(expectedPath, DUMMY_SHA); | ||
}); | ||
|
||
test("directory ListFileEntry should mkdir it", async () => { | ||
vi.mocked(listFiles).mockReturnValue( | ||
toAsyncGenerator([ | ||
{ | ||
oid: "dummy-etag", | ||
type: "directory", | ||
path: "potatoes", | ||
size: 0, | ||
lastCommit: { | ||
date: new Date().toISOString(), | ||
id: DUMMY_SHA, | ||
title: "feat: best commit", | ||
}, | ||
}, | ||
]) | ||
); | ||
|
||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
}); | ||
|
||
// cross-platform testing | ||
const expectedPath = join(getHFHubCachePath(), "spaces--foo--bar", "snapshots", DUMMY_SHA, "potatoes"); | ||
expect(mkdir).toHaveBeenCalledWith(expectedPath, { recursive: true }); | ||
}); | ||
|
||
test("files in ListFileEntry should download them", async () => { | ||
const entries: ListFileEntry[] = Array.from({ length: 10 }, (_, i) => ({ | ||
oid: `dummy-etag-${i}`, | ||
type: "file", | ||
path: `file-${i}.txt`, | ||
size: i, | ||
lastCommit: { | ||
date: new Date().toISOString(), | ||
id: DUMMY_SHA, | ||
title: "feat: best commit", | ||
}, | ||
})); | ||
vi.mocked(listFiles).mockReturnValue(toAsyncGenerator(entries)); | ||
|
||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
}); | ||
|
||
for (const entry of entries) { | ||
expect(downloadFileToCacheDir).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
path: entry.path, | ||
revision: DUMMY_SHA, | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
test("custom params should be propagated", async () => { | ||
// fetch mock | ||
const fetchMock: typeof fetch = vi.fn(); | ||
const hubMock = "https://foor.bar"; | ||
const accessTokenMock = "dummy-access-token"; | ||
|
||
vi.mocked(listFiles).mockReturnValue( | ||
toAsyncGenerator([ | ||
{ | ||
oid: `dummy-etag`, | ||
type: "file", | ||
path: `file.txt`, | ||
size: 10, | ||
lastCommit: { | ||
date: new Date().toISOString(), | ||
id: DUMMY_SHA, | ||
title: "feat: best commit", | ||
}, | ||
}, | ||
]) | ||
); | ||
|
||
await snapshotDownload({ | ||
repo: { | ||
name: "foo/bar", | ||
type: "space", | ||
}, | ||
hubUrl: hubMock, | ||
fetch: fetchMock, | ||
accessToken: accessTokenMock, | ||
}); | ||
|
||
expect(spaceInfo).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
fetch: fetchMock, | ||
hubUrl: hubMock, | ||
accessToken: accessTokenMock, | ||
}) | ||
); | ||
|
||
// list files should receive custom fetch | ||
expect(listFiles).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
fetch: fetchMock, | ||
hubUrl: hubMock, | ||
accessToken: accessTokenMock, | ||
}) | ||
); | ||
|
||
// download file to cache should receive custom fetch | ||
expect(downloadFileToCacheDir).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
fetch: fetchMock, | ||
hubUrl: hubMock, | ||
accessToken: accessTokenMock, | ||
}) | ||
); | ||
}); | ||
}); |
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.