-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add e2e test for script with deps
- Loading branch information
Showing
12 changed files
with
184 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 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,8 @@ | ||
[package] | ||
name="lib_a" | ||
type="lib" | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] | ||
lib_b = { path = "../lib-b" } |
This file contains 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,7 @@ | ||
|
||
use dep::lib_b::assert_non_zero; | ||
|
||
pub fn divide(a: u64, b: u64) -> u64 { | ||
assert_non_zero(b); | ||
a / b | ||
} |
This file contains 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,7 @@ | ||
[package] | ||
name="lib_b" | ||
type="lib" | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
This file contains 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,4 @@ | ||
|
||
pub fn assert_non_zero(x: u64) { | ||
assert(x != 0); | ||
} |
This file contains 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,8 @@ | ||
[package] | ||
name="noir_wasm_testing" | ||
type="bin" | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] | ||
lib_a = { path="../lib-a" } |
This file contains 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,4 @@ | ||
use dep::lib_a::divide; | ||
fn main(x : u64, y : pub u64) { | ||
divide(x, y); | ||
} |
File renamed without changes.
File renamed without changes.
This file contains 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,68 +1,98 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import initNoirWasm, { compile } from '@noir-lang/noir_wasm'; | ||
import { initializeResolver } from '@noir-lang/source-resolver'; | ||
import { nargoArtifactPath, noirSourcePath } from '../shared'; | ||
import { | ||
depsScriptExpectedArtifact, | ||
depsScriptSourcePath, | ||
libASourcePath, | ||
libBSourcePath, | ||
simpleScriptExpectedArtifact, | ||
simpleScriptSourcePath, | ||
} from '../shared'; | ||
|
||
beforeEach(async () => { | ||
await initNoirWasm(); | ||
}); | ||
|
||
async function getFileContent(path: string): Promise<string> { | ||
const mainnrSourceURL = new URL(path, import.meta.url); | ||
const response = await fetch(mainnrSourceURL); | ||
const url = new URL(path, import.meta.url); | ||
const response = await fetch(url); | ||
return await response.text(); | ||
} | ||
|
||
async function getSource(): Promise<string> { | ||
return getFileContent(noirSourcePath); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async function getPrecompiledSource(): Promise<any> { | ||
const compiledData = await getFileContent(nargoArtifactPath); | ||
async function getPrecompiledSource(path: string): Promise<any> { | ||
const compiledData = await getFileContent(path); | ||
return JSON.parse(compiledData); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export async function compileNoirSource(noir_source: string): Promise<any> { | ||
console.log('Compiling Noir source...'); | ||
describe('noir wasm', () => { | ||
describe('can compile script without dependencies', () => { | ||
beforeEach(async () => { | ||
const source = await getFileContent(simpleScriptSourcePath); | ||
initializeResolver((id: string) => { | ||
console.log(`Resolving source ${id}`); | ||
|
||
initializeResolver((id: string) => { | ||
console.log(`Resolving source ${id}`); | ||
if (typeof source === 'undefined') { | ||
throw Error(`Could not resolve source for '${id}'`); | ||
} else if (id !== '/main.nr') { | ||
throw Error(`Unexpected id: '${id}'`); | ||
} else { | ||
return source; | ||
} | ||
}); | ||
}); | ||
|
||
const source = noir_source; | ||
it('matching nargos compilation', async () => { | ||
const wasmCircuit = await compile('/main.nr'); | ||
const cliCircuit = await getPrecompiledSource(simpleScriptExpectedArtifact); | ||
|
||
if (typeof source === 'undefined') { | ||
throw Error(`Could not resolve source for '${id}'`); | ||
} else if (id !== '/main.nr') { | ||
throw Error(`Unexpected id: '${id}'`); | ||
} else { | ||
return source; | ||
} | ||
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies | ||
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode); | ||
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi); | ||
expect(wasmCircuit.backend).to.eq(cliCircuit.backend); | ||
}).timeout(20e3); // 20 seconds | ||
}); | ||
|
||
try { | ||
const compiled_noir = compile('main.nr'); | ||
describe('can compile script with dependencies', () => { | ||
beforeEach(async () => { | ||
const [scriptSource, libASource, libBSource] = await Promise.all([ | ||
getFileContent(depsScriptSourcePath), | ||
getFileContent(libASourcePath), | ||
getFileContent(libBSourcePath), | ||
]); | ||
|
||
console.log('Noir source compilation done.'); | ||
initializeResolver((file: string) => { | ||
switch (file) { | ||
case '/script/main.nr': | ||
return scriptSource; | ||
|
||
return compiled_noir; | ||
} catch (e) { | ||
console.log('Error while compiling:', e); | ||
} | ||
} | ||
case '/lib_a/lib.nr': | ||
return libASource; | ||
|
||
case '/lib_b/lib.nr': | ||
return libBSource; | ||
|
||
describe('noir wasm compilation', () => { | ||
it('matches nargos compilation', async () => { | ||
const source = await getSource(); | ||
default: | ||
return ''; | ||
} | ||
}); | ||
}); | ||
|
||
const wasmCircuit = await compileNoirSource(source); | ||
it('matching nargos compilation', async () => { | ||
const wasmCircuit = await compile('/script/main.nr', false, { | ||
root_dependencies: ['lib_a'], | ||
library_dependencies: { | ||
lib_a: ['lib_b'], | ||
}, | ||
}); | ||
|
||
const cliCircuit = await getPrecompiledSource(); | ||
const cliCircuit = await getPrecompiledSource(depsScriptExpectedArtifact); | ||
|
||
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies | ||
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode); | ||
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi); | ||
expect(wasmCircuit.backend).to.eq(cliCircuit.backend); | ||
}).timeout(20e3); // 20 seconds | ||
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies | ||
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode); | ||
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi); | ||
expect(wasmCircuit.backend).to.eq(cliCircuit.backend); | ||
}).timeout(20e3); // 20 seconds | ||
}); | ||
}); |
This file contains 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,26 +1,72 @@ | ||
import { expect } from 'chai'; | ||
import { nargoArtifactPath, noirSourcePath } from '../shared'; | ||
import { | ||
depsScriptSourcePath, | ||
depsScriptExpectedArtifact, | ||
libASourcePath, | ||
libBSourcePath, | ||
simpleScriptSourcePath, | ||
simpleScriptExpectedArtifact, | ||
} from '../shared'; | ||
import { readFileSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import { join, resolve } from 'node:path'; | ||
import { compile } from '@noir-lang/noir_wasm'; | ||
import { initializeResolver } from '@noir-lang/source-resolver'; | ||
|
||
const absoluteNoirSourcePath = join(__dirname, noirSourcePath); | ||
const absoluteNargoArtifactPath = join(__dirname, nargoArtifactPath); | ||
// const absoluteNoirSourcePath = ; | ||
// const absoluteNargoArtifactPath = join(__dirname, nargoArtifactPath); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async function getPrecompiledSource(): Promise<any> { | ||
const compiledData = readFileSync(absoluteNargoArtifactPath).toString(); | ||
async function getPrecompiledSource(path: string): Promise<any> { | ||
const compiledData = readFileSync(resolve(__dirname, path)).toString(); | ||
return JSON.parse(compiledData); | ||
} | ||
|
||
describe('noir wasm compilation', () => { | ||
it('matches nargos compilation', async () => { | ||
const wasmCircuit = await compile(absoluteNoirSourcePath); | ||
const cliCircuit = await getPrecompiledSource(); | ||
|
||
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies | ||
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode); | ||
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi); | ||
expect(wasmCircuit.backend).to.eq(cliCircuit.backend); | ||
}).timeout(10e3); | ||
describe('can compile simple scripts', () => { | ||
it('matching nargos compilation', async () => { | ||
const wasmCircuit = await compile(join(__dirname, simpleScriptSourcePath)); | ||
const cliCircuit = await getPrecompiledSource(simpleScriptExpectedArtifact); | ||
|
||
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies | ||
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode); | ||
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi); | ||
expect(wasmCircuit.backend).to.eq(cliCircuit.backend); | ||
}).timeout(10e3); | ||
}); | ||
|
||
describe('can compile scripts with dependencies', () => { | ||
beforeEach(() => { | ||
initializeResolver((file) => { | ||
switch (file) { | ||
case '/script/main.nr': | ||
return readFileSync(join(__dirname, depsScriptSourcePath), 'utf-8'); | ||
|
||
case '/lib_a/lib.nr': | ||
return readFileSync(join(__dirname, libASourcePath), 'utf-8'); | ||
|
||
case '/lib_b/lib.nr': | ||
return readFileSync(join(__dirname, libBSourcePath), 'utf-8'); | ||
|
||
default: | ||
return ''; | ||
} | ||
}); | ||
}); | ||
|
||
it('matching nargos compilation', async () => { | ||
const wasmCircuit = await compile('/script/main.nr', false, { | ||
root_dependencies: ['lib_a'], | ||
library_dependencies: { | ||
lib_a: ['lib_b'], | ||
}, | ||
}); | ||
|
||
const cliCircuit = await getPrecompiledSource(depsScriptExpectedArtifact); | ||
|
||
// We don't expect the hashes to match due to how `noir_wasm` handles dependencies | ||
expect(wasmCircuit.bytecode).to.eq(cliCircuit.bytecode); | ||
expect(wasmCircuit.abi).to.deep.eq(cliCircuit.abi); | ||
expect(wasmCircuit.backend).to.eq(cliCircuit.backend); | ||
}).timeout(10e3); | ||
}); | ||
}); |
This file contains 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,2 +1,8 @@ | ||
export const noirSourcePath = '../../noir-script/src/main.nr'; | ||
export const nargoArtifactPath = '../../noir-script/target/noir_wasm_testing.json'; | ||
export const simpleScriptSourcePath = '../../fixtures/simple/noir-script/src/main.nr'; | ||
export const simpleScriptExpectedArtifact = '../../fixtures/simple/noir-script/target/noir_wasm_testing.json'; | ||
|
||
export const depsScriptSourcePath = '../../fixtures/deps/noir-script/src/main.nr'; | ||
export const depsScriptExpectedArtifact = '../../fixtures/deps/noir-script/target/noir_wasm_testing.json'; | ||
|
||
export const libASourcePath = '../../fixtures/deps/lib-a/src/lib.nr'; | ||
export const libBSourcePath = '../../fixtures/deps/lib-b/src/lib.nr'; |