Skip to content

Commit

Permalink
refactor: inject fetcher instead of using global (#5502)
Browse files Browse the repository at this point in the history
This PR refactors the GithubDependencyResolver to take a fetch
implementation instead of always using the global default
  • Loading branch information
alexghr authored Mar 28, 2024
1 parent bea3fcb commit a066544
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { LogData } from '../../utils';
*/
export class GithubDependencyResolver implements DependencyResolver {
#fm: FileManager;
#fetch: typeof fetch;
#log;

constructor(fm: FileManager) {
constructor(fm: FileManager, fetcher: typeof fetch) {
this.#fm = fm;
this.#fetch = fetcher;
this.#log = (msg: string, _data?: LogData) => {
console.log(msg);
};
Expand Down Expand Up @@ -56,7 +58,7 @@ export class GithubDependencyResolver implements DependencyResolver {
return localArchivePath;
}

const response = await fetch(url, {
const response = await this.#fetch(url, {
method: 'GET',
});

Expand Down
3 changes: 2 additions & 1 deletion noir/noir-repo/compiler/wasm/src/noir/noir-wasm-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class NoirWasmCompiler {
const dependencyManager = new DependencyManager(
[
new LocalDependencyResolver(fileManager),
new GithubCodeArchiveDependencyResolver(fileManager),
// use node's global fetch
new GithubCodeArchiveDependencyResolver(fileManager, fetch),
// TODO support actual Git repositories
],
noirPackage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('GithubDependencyResolver', () => {
let fetchStub: SinonStub | undefined;

beforeEach(() => {
fetchStub = Sinon.stub(globalThis, 'fetch');
fetchStub = Sinon.stub();
fm = createMemFSFileManager(createFsFromVolume(new Volume()), '/');

libDependency = {
Expand All @@ -50,16 +50,12 @@ describe('GithubDependencyResolver', () => {
},
});

resolver = new GithubDependencyResolver(fm);
resolver = new GithubDependencyResolver(fm, fetchStub);

// cut off outside access
fetchStub.onCall(0).throws(new Error());
});

afterEach(() => {
fetchStub?.restore();
});

it("returns null if it can't resolve a dependency", async () => {
const dep = await resolver.resolveDependency(pkg, {
path: '/lib-c',
Expand Down

0 comments on commit a066544

Please sign in to comment.