-
-
Notifications
You must be signed in to change notification settings - Fork 5
test: expand FileSystem tests #97
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
Open
outslept
wants to merge
3
commits into
e18e:main
Choose a base branch
from
outslept:test/file-systems
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,84 @@ | ||
| import {describe, it, expect, beforeEach, afterEach} from 'vitest'; | ||
| import {TarballFileSystem} from '../tarball-file-system.js'; | ||
| import {detectAndPack} from '../detect-and-pack-node.js'; | ||
| import * as fs from 'node:fs/promises'; | ||
| import {describe, it, expect, beforeEach, vi} from 'vitest'; | ||
| import * as path from 'node:path'; | ||
| import {tmpdir} from 'node:os'; | ||
|
|
||
| import {TarballFileSystem} from '../tarball-file-system.js'; | ||
|
|
||
| const enc = (s: string) => new TextEncoder().encode(s); | ||
| let mockFiles: Array<{name: string; data: Uint8Array}>; | ||
| const mockRootDir = 'package'; | ||
|
|
||
| vi.mock('@publint/pack', () => { | ||
| return { | ||
| unpack: vi.fn(async () => ({ | ||
| rootDir: mockRootDir, | ||
| files: mockFiles | ||
| })) | ||
| }; | ||
| }); | ||
|
|
||
| describe('TarballFileSystem', () => { | ||
| let tempDir: string; | ||
| beforeEach(() => { | ||
| mockFiles = [ | ||
| { | ||
| name: path.posix.join(mockRootDir, 'package.json'), | ||
| data: enc(JSON.stringify({name: 'pkg', version: '1.0.0'})) | ||
| }, | ||
| { | ||
| name: path.posix.join(mockRootDir, 'tsconfig.json'), | ||
| data: enc('{}') | ||
| }, | ||
| { | ||
| name: path.posix.join(mockRootDir, 'node_modules/a/package.json'), | ||
| data: enc(JSON.stringify({name: 'a', version: '1.0.0'})) | ||
| }, | ||
| { | ||
| name: path.posix.join( | ||
| mockRootDir, | ||
| 'node_modules/a/node_modules/b/package.json' | ||
| ), | ||
| data: enc(JSON.stringify({name: 'b', version: '1.0.0'})) | ||
| }, | ||
| { | ||
| name: path.posix.join(mockRootDir, 'node_modules/a/readme.txt'), | ||
| data: enc('abc') | ||
| } | ||
| ]; | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| tempDir = await fs.mkdtemp(path.join(tmpdir(), 'reporter-test-')); | ||
| it('should report true for an existing file and false for a missing file', async () => { | ||
| const tfs = new TarballFileSystem(new ArrayBuffer(0)); | ||
|
|
||
| expect(await tfs.fileExists('/tsconfig.json')).toBe(true); | ||
| expect(await tfs.fileExists('/does-not-exist.json')).toBe(false); | ||
| }); | ||
|
|
||
| it('should read /package.json and throw on a non-existent path', async () => { | ||
| const tfs = new TarballFileSystem(new ArrayBuffer(0)); | ||
|
|
||
| const text = await tfs.readFile('/package.json'); | ||
| expect(JSON.parse(text).name).toBe('pkg'); | ||
|
|
||
| await expect(tfs.readFile('/nope.json')).rejects.toBeTruthy(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(tempDir, {recursive: true, force: true}); | ||
| it('should list package.json files, including nested ones', async () => { | ||
| const tfs = new TarballFileSystem(new ArrayBuffer(0)); | ||
| const root = await tfs.getRootDir(); | ||
| const files = await tfs.listPackageFiles(); | ||
|
|
||
| expect(files).toContain(path.posix.join(root, 'package.json')); | ||
| expect(files).toContain( | ||
| path.posix.join(root, 'node_modules/a/package.json') | ||
| ); | ||
| expect(files).toContain( | ||
| path.posix.join(root, 'node_modules/a/node_modules/b/package.json') | ||
| ); | ||
| }); | ||
|
|
||
| describe('fileExists', () => { | ||
| it('should return false when file does not exist in tarball', async () => { | ||
| // Create a minimal package.json for the tarball | ||
| await fs.writeFile( | ||
| path.join(tempDir, 'package.json'), | ||
| JSON.stringify({ | ||
| name: 'test-package', | ||
| version: '1.0.0' | ||
| }) | ||
| ); | ||
|
|
||
| const tarball = await detectAndPack(tempDir, 'npm'); | ||
| const fileSystem = new TarballFileSystem(tarball); | ||
| const hasConfig = await fileSystem.fileExists('/tsconfig.json'); | ||
| expect(hasConfig).toBe(false); | ||
| }); | ||
|
|
||
| it('should return true when file exists in tarball', async () => { | ||
| // Create a minimal package.json for the tarball | ||
| await fs.writeFile( | ||
| path.join(tempDir, 'package.json'), | ||
| JSON.stringify({ | ||
| name: 'test-package', | ||
| version: '1.0.0' | ||
| }) | ||
| ); | ||
|
|
||
| await fs.writeFile(path.join(tempDir, 'tsconfig.json'), '{}'); | ||
| const tarball = await detectAndPack(tempDir, 'npm'); | ||
| const fileSystem = new TarballFileSystem(tarball); | ||
| const hasConfig = await fileSystem.fileExists('/tsconfig.json'); | ||
| expect(hasConfig).toBe(true); | ||
| }); | ||
| it('should compute install size as the sum of file bytes from the unpacked tarball', async () => { | ||
| const tfs = new TarballFileSystem(new ArrayBuffer(0)); | ||
| const expected = mockFiles.reduce((n, f) => n + f.data.byteLength, 0); | ||
| const size = await tfs.getInstallSize(); | ||
| expect(size).toBe(expected); | ||
| }); | ||
| }); |
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.
this should probably be two tests
Uh oh!
There was an error while loading. Please reload this page.
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.
looking at it, this and the
readFiletests were probably organised better before, no?roughly:
fileExistsreadFilesame with the listPackageFiles tests, they should have their own block i think
listPackageFilesand
getInstallSize:getInstallSize