Repository: isomorphic-git/isomorphic-git
Pull request #1858 (2024-01-22 21:52:58)

(chore) Fix unit tests
<!-- Oh wow! Thanks for opening a pull request! 😁 🎉 -->
<!-- You are very welcome here and any contribution is appreciated. 👍 -->
<!-- Choose one of the checklists if it applies to you and delete the rest. -->

## I'm fixing a bug or typo

- [ ] if this is your first time contributing, run `npm run add-contributor` and follow the prompts to add yourself to the README
- [ ] squash merge the PR with commit message "fix: [Description of fix]"

## I'm adding a parameter to an existing command X:

- [ ] add parameter to the function in `src/api/X.js` (and `src/commands/X.js` if necessary)
- [ ] document the parameter in the JSDoc comment above the function
- [ ] add a test case in `__tests__/test-X.js` if possible
- [ ] if this is your first time contributing, run `npm run add-contributor` and follow the prompts to add yourself to the README
- [ ] squash merge the PR with commit message "feat(X): Added 'bar' parameter"

## I'm adding a new command:

- [ ] add as a new file in `src/api` (and `src/commands` if necessary)
- [ ] add command to `src/index.js`
- [ ] update `__tests__/test-exports.js`
- [ ] create a test in `src/__tests__`
- [ ] document the command with a JSDoc comment
- [ ] add page to the Docs Sidebar `website/sidebars.json`
- [ ] add page to the v1 Docs Sidebar `website/versioned_sidebars/version-1.x-sidebars.json`
- [ ] if this is your first time contributing, run `npm run add-contributor` and follow the prompts to add yourself to the README
- [ ] squash merge the PR with commit message "feat: Added 'X' command"


jcubic: I give up fix the build. Adding one module from ./src to __tests__ file show a few typeScript errors removing __tests__ from TypeScript `inlucde` config gives hundreds of TypeScript errors.

Patch:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 20b59f82..c01f8458 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -95,8 +95,11 @@ And here is another advanced video:<br/>
 Another resource is GitHub blog:
 * [Git’s database internals I: packed object store](https://github.blog/2022-08-29-gits-database-internals-i-packed-object-store/)
 * [Git’s database internals II: commit history queries](https://github.blog/2022-08-30-gits-database-internals-ii-commit-history-queries/)
-* 
+
 And this description of .git directory:
 * [What is in that .git directory?](https://blog.meain.io/2023/what-is-in-dot-git/)
 
+There is also chapter in git Pro book
+* [Git Internals](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain)
+
 You can also search [git in the blog of Julia Evans](https://duckduckgo.com/?q=site%3Ajvns.ca+git&ia=web).
diff --git a/src/internal-apis.d.ts b/src/internal-apis.d.ts
index 17bb85a5..b173461e 100644
--- a/src/internal-apis.d.ts
+++ b/src/internal-apis.d.ts
@@ -1,1 +1,10 @@
 declare module 'isomorphic-git/internal-apis'
+
+
+// ref: https://stackoverflow.com/a/77801519/387194
+interface CompressionStream extends GenericTransformStream {  }
+type CompressionFormat = "deflate" | "deflate-raw" | "gzip";
+declare var CompressionStream: {
+  prototype: CompressionStream;
+  new(format: CompressionFormat): CompressionStream
+}
diff --git a/src/internal-apis.js b/src/internal-apis.js
index 625c0309..a5fcba74 100644
--- a/src/internal-apis.js
+++ b/src/internal-apis.js
@@ -29,6 +29,7 @@ export * from './models/GitTree'
 
 export * from './storage/readObject'
 export * from './storage/writeObject'
+export * from './storage/readObjectPacked'
 
 export * from './utils/calculateBasicAuthHeader'
 export * from './utils/collect'
diff --git a/tsconfig.json b/tsconfig.json
index 3b259bca..1a85db9f 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -22,4 +22,4 @@
       "isomorphic-git/http": ["./http/node/index.d.ts"]
     }
   }
-}
\ No newline at end of file
+}


Test patch:
diff --git a/__tests__/__fixtures__/test-packObjects/foobar-76178ca22ef818f971fca371d84bce571d474b1d.idx b/__tests__/__fixtures__/test-packObjects/git/objects/pack/pack-76178ca22ef818f971fca371d84bce571d474b1d.idx
similarity index 100%
rename from __tests__/__fixtures__/test-packObjects/foobar-76178ca22ef818f971fca371d84bce571d474b1d.idx
rename to __tests__/__fixtures__/test-packObjects/git/objects/pack/pack-76178ca22ef818f971fca371d84bce571d474b1d.idx
diff --git a/__tests__/__fixtures__/test-packObjects/foobar-76178ca22ef818f971fca371d84bce571d474b1d.pack b/__tests__/__fixtures__/test-packObjects/git/objects/pack/pack-76178ca22ef818f971fca371d84bce571d474b1d.pack
similarity index 100%
rename from __tests__/__fixtures__/test-packObjects/foobar-76178ca22ef818f971fca371d84bce571d474b1d.pack
rename to __tests__/__fixtures__/test-packObjects/git/objects/pack/pack-76178ca22ef818f971fca371d84bce571d474b1d.pack
diff --git a/__tests__/test-packObjects.js b/__tests__/test-packObjects.js
index ec560735..8c6bbb35 100644
--- a/__tests__/test-packObjects.js
+++ b/__tests__/test-packObjects.js
@@ -1,18 +1,15 @@
 /* eslint-env node, browser, jasmine */
 const path = require('path')
 
-const { packObjects } = require('isomorphic-git')
+const { packObjects, indexPack } = require('isomorphic-git')
+const { readObject, readObjectPacked } = require('isomorphic-git/internal-apis')
 
 const { makeFixture } = require('./__helpers__/FixtureFS.js')
 
 describe('packObjects', () => {
   it('makes a packfile', async () => {
     // Setup
-    const { fs, dir, gitdir } = await makeFixture('test-packObjects')
-    // Test
-    const fixture = await fs.read(
-      path.join(dir, 'foobar-76178ca22ef818f971fca371d84bce571d474b1d.pack')
-    )
+    const { fs, gitdir } = await makeFixture('test-packObjects')
     const { filename, packfile } = await packObjects({
       fs,
       gitdir,
@@ -31,49 +28,64 @@ describe('packObjects', () => {
         '5477471ab5a6a8f2c217023532475044117a8f2c',
       ],
     })
-    expect(filename).toBe('pack-76178ca22ef818f971fca371d84bce571d474b1d.pack')
     if (!packfile) throw new Error('type error')
-    expect(fixture.buffer).toEqual(packfile.buffer)
-    expect(
-      Buffer.compare(Buffer.from(fixture), Buffer.from(packfile)) === 0
-    ).toBe(true)
     expect(await fs.exists(path.join(gitdir, `objects/pack/${filename}`))).toBe(
       false
     )
   })
-  it('saves packfile to disk', async () => {
+  it('save packfile', async () => {
     // Setup
     const { fs, dir, gitdir } = await makeFixture('test-packObjects')
-    // Test
-    const fixture = await fs.read(
-      path.join(dir, 'foobar-76178ca22ef818f971fca371d84bce571d474b1d.pack')
-    )
+    const oids = [
+      '5a9da3272badb2d3c8dbab463aed5741acb15a33',
+      '0bfe8fa3764089465235461624f2ede1533e74ec',
+      '414a0afa7e20452d90ab52de1c024182531c5c52',
+      '97b32c43e96acc7873a1990e409194cb92421522',
+      '328e74b65839f7e5a8ae3b54e0b49180a5b7b82b',
+      'fdba2ad440c231d15a2179f729b4b50ab5860df2',
+      '5171f8a8291d7edc31a6670800d5967cfd6be830',
+      '7983b4770a894a068152dfe6f347ea9b5ae561c5',
+      'f03ae7b490022507f83729b9227e723ab1587a38',
+      'a59efbcd7640e659ec81887a2599711f8d9ef801',
+      'e5abf40a5b37382c700f51ac5c2aeefdadb8e184',
+      '5477471ab5a6a8f2c217023532475044117a8f2c',
+    ]
     const { filename } = await packObjects({
       fs,
       gitdir,
-      oids: [
-        '5a9da3272badb2d3c8dbab463aed5741acb15a33',
-        '0bfe8fa3764089465235461624f2ede1533e74ec',
-        '414a0afa7e20452d90ab52de1c024182531c5c52',
-        '97b32c43e96acc7873a1990e409194cb92421522',
-        '328e74b65839f7e5a8ae3b54e0b49180a5b7b82b',
-        'fdba2ad440c231d15a2179f729b4b50ab5860df2',
-        '5171f8a8291d7edc31a6670800d5967cfd6be830',
-        '7983b4770a894a068152dfe6f347ea9b5ae561c5',
-        'f03ae7b490022507f83729b9227e723ab1587a38',
-        'a59efbcd7640e659ec81887a2599711f8d9ef801',
-        'e5abf40a5b37382c700f51ac5c2aeefdadb8e184',
-        '5477471ab5a6a8f2c217023532475044117a8f2c',
-      ],
+      oids,
       write: true,
     })
-    expect(filename).toBe('pack-76178ca22ef818f971fca371d84bce571d474b1d.pack')
-    const filepath = path.join(gitdir, `objects/pack/${filename}`)
-    expect(await fs.exists(filepath)).toBe(true)
-    const packfile = await fs.read(filepath)
-    expect(fixture.buffer).toEqual(packfile.buffer)
-    expect(
-      Buffer.compare(Buffer.from(fixture), Buffer.from(packfile)) === 0
-    ).toBe(true)
+    const filepath = `objects/pack/${filename}`
+    const cache = {}
+    const fixcache = {}
+    const fixdir = path.join(dir, 'git')
+    const fullpath = path.join(gitdir, filepath)
+    expect(await fs.exists(fullpath)).toBe(true)
+    const getExternalRefDelta = oid => readObject({ fs, cache, gitdir, oid })
+    await indexPack({ fs, dir: gitdir, filepath, gitdir, cache })
+    await Promise.all(
+      oids.map(async oid => {
+        const object = await readObjectPacked({
+          fs,
+          gitdir,
+          oid,
+          cache,
+          getExternalRefDelta,
+        })
+        const fixture = await readObjectPacked({
+          fs,
+          gitdir: fixdir,
+          oid,
+          getExternalRefDelta,
+          cache: fixcache,
+        })
+        // the packfile can have different source when compression changes
+        // There are no guarantee that the compression will always give same result
+        delete object.source
+        delete fixture.source
+        expect(object).toEqual(fixture)
+      })
+    )
   })
 })