Skip to content

Commit

Permalink
Merge branch 'master' into tf/deduplicate-abi-code
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Oct 9, 2023
2 parents 760a469 + 1f10395 commit e7f8f58
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 106 deletions.
1 change: 0 additions & 1 deletion compiler/integration-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "integration-tests",
"version": "1.0.0",
"license": "(MIT OR Apache-2.0)",
"main": "index.js",
"private": true,
Expand Down
4 changes: 2 additions & 2 deletions compiler/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"scripts": {
"build": "bash ./build.sh",
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test": "yarn test:node && yarn test:browser",
"test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' mocha",
"test:browser": "web-test-runner",
"clean": "chmod u+w web nodejs || true && rm -rf ./nodejs ./web ./target ./result",
"publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish",
Expand Down
34 changes: 32 additions & 2 deletions compiler/wasm/test/browser/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from '@esm-bundle/chai';
import initNoirWasm from '@noir-lang/noir_wasm';
import { compileNoirSource, nargoArtifactPath, noirSourcePath } from '../shared';
import initNoirWasm, { compile } from '@noir-lang/noir_wasm';
import { initializeResolver } from '@noir-lang/source-resolver';
import { nargoArtifactPath, noirSourcePath } from '../shared';

beforeEach(async () => {
await initNoirWasm();
Expand All @@ -22,6 +23,35 @@ async function getPrecompiledSource(): Promise<any> {
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...');

initializeResolver((id: string) => {
console.log(`Resolving source ${id}`);

const source = noir_source;

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;
}
});

try {
const compiled_noir = compile('main.nr');

console.log('Noir source compilation done.');

return compiled_noir;
} catch (e) {
console.log('Error while compiling:', e);
}
}

describe('noir wasm compilation', () => {
it('matches nargos compilation', async () => {
const source = await getSource();
Expand Down
1 change: 0 additions & 1 deletion compiler/wasm/test/index.d.ts

This file was deleted.

19 changes: 6 additions & 13 deletions compiler/wasm/test/node/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import { expect } from 'chai';
import { compileNoirSource, nargoArtifactPath, noirSourcePath } from '../shared';
import { nargoArtifactPath, noirSourcePath } from '../shared';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { compile } from '@noir-lang/noir_wasm';

async function getFileContent(path: string): Promise<string> {
return readFileSync(join(__dirname, path)).toString();
}

async function getSource(): Promise<string> {
return getFileContent(noirSourcePath);
}
const absoluteNoirSourcePath = join(__dirname, noirSourcePath);
const absoluteNargoArtifactPath = join(__dirname, nargoArtifactPath);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function getPrecompiledSource(): Promise<any> {
const compiledData = await getFileContent(nargoArtifactPath);
const compiledData = readFileSync(absoluteNargoArtifactPath).toString();
return JSON.parse(compiledData);
}

describe('noir wasm compilation', () => {
it('matches nargos compilation', async () => {
const source = await getSource();

const wasmCircuit = await compileNoirSource(source);

const wasmCircuit = await compile(absoluteNoirSourcePath);
const cliCircuit = await getPrecompiledSource();

// We don't expect the hashes to match due to how `noir_wasm` handles dependencies
Expand Down
32 changes: 0 additions & 32 deletions compiler/wasm/test/shared.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,2 @@
import { initializeResolver } from '@noir-lang/source-resolver';
import { compile } from '@noir-lang/noir_wasm';

export const noirSourcePath = '../../noir-script/src/main.nr';
export const nargoArtifactPath = '../../noir-script/target/noir_wasm_testing.json';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function compileNoirSource(noir_source: string): Promise<any> {
console.log('Compiling Noir source...');

initializeResolver((id: string) => {
console.log(`Resolving source ${id}`);

const source = noir_source;

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;
}
});

try {
const compiled_noir = compile('main.nr');

console.log('Noir source compilation done.');

return compiled_noir;
} catch (e) {
console.log('Error while compiling:', e);
}
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
# Custom filter with various file extensions that we rely upon to build packages
# Currently: `.nr`, `.sol`, `.sh`, `.json`, `.md`
filter = path: type:
(builtins.match ".*\.(sh|json|md)$" path != null) || (craneLib.filterCargoSources path type);
(builtins.match ".*\.(nr|sol|sh|json|md)$" path != null) || (craneLib.filterCargoSources path type);
};

# TODO(#1198): It'd be nice to include these flags when running `cargo clippy` in a devShell.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@noir-lang/root",
"version": "0.11.1",
"private": true,
"workspaces": [
"compiler/wasm",
Expand Down
16 changes: 13 additions & 3 deletions release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"flake.nix",
{
"type": "json",
"path": "compiler/wasm/package.json",
"path": "compiler/source-resolver/package.json",
"jsonpath": "$.version"
},
{
"type": "json",
"path": "tooling/noirc_abi_wasm/package.json",
"path": "compiler/wasm/package.json",
"jsonpath": "$.version"
},
{
Expand All @@ -31,7 +31,17 @@
},
{
"type": "json",
"path": "compiler/source-resolver/package.json",
"path": "tooling/noir_js_backend_barretenberg/package.json",
"jsonpath": "$.version"
},
{
"type": "json",
"path": "tooling/noir_js_types/package.json",
"jsonpath": "$.version"
},
{
"type": "json",
"path": "tooling/noirc_abi_wasm/package.json",
"jsonpath": "$.version"
}
]
Expand Down
1 change: 0 additions & 1 deletion release-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "release-tests",
"version": "0.0.0",
"main": "index.js",
"license": "(MIT OR Apache-2.0)",
"private": true,
Expand Down
68 changes: 68 additions & 0 deletions tooling/nargo_fmt/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::{env, fs};

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let destination = Path::new(&out_dir).join("execute.rs");
let mut test_file = File::create(destination).unwrap();

// Try to find the directory that Cargo sets when it is running; otherwise fallback to assuming the CWD
// is the root of the repository and append the crate path
let manifest_dir = match std::env::var("CARGO_MANIFEST_DIR") {
Ok(dir) => PathBuf::from(dir),
Err(_) => std::env::current_dir().unwrap().join("crates").join("nargo_cli"),
};
let test_dir = manifest_dir.join("tests");

generate_formatter_tests(&mut test_file, &test_dir);
}

fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) {
let inputs_dir = test_data_dir.join("input");
let outputs_dir = test_data_dir.join("expected");

let test_case_files =
fs::read_dir(inputs_dir).unwrap().flatten().filter(|c| c.path().is_file());

for file in test_case_files {
let file_path = file.path();
let file_name = file_path.file_name().unwrap();
let test_name = file_path.file_stem().unwrap().to_str().unwrap();

if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};

let input_source_path = file.path();
let input_source = std::fs::read_to_string(input_source_path).unwrap();

let output_source_path = outputs_dir.join(file_name);
let output_source = std::fs::read_to_string(output_source_path).unwrap();

write!(
test_file,
r##"
#[test]
fn format_{test_name}() {{
let input = r#"{input_source}"#;
let expected_output = r#"{output_source}"#;
let (parsed_module, errors) = noirc_frontend::parse_program(&input);
assert!(errors.is_empty());
let config = nargo_fmt::Config::default();
let fmt_text = nargo_fmt::format(&input, parsed_module, &config);
assert_eq!(fmt_text, expected_output);
}}
"##
)
.expect("Could not write templated test file.");
}
}
48 changes: 0 additions & 48 deletions tooling/nargo_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,3 @@ pub fn format(source: &str, parsed_module: ParsedModule, config: &Config) -> Str
fmt.visit_module(parsed_module);
fmt.finish()
}

#[cfg(test)]
mod tests {
use std::{ffi::OsStr, path::PathBuf};

use crate::Config;

#[test]
fn test() {
let files = std::fs::read_dir("tests/input").unwrap();
for file in files {
let file = file.unwrap();

let config = Config::default();

let source_path = file.path();
let source = std::fs::read_to_string(&source_path).unwrap();

let (parsed_module, errors) = noirc_frontend::parse_program(&source);
let fmt_text = crate::format(&source, parsed_module, &config);

assert!(errors.is_empty());

let target_path: PathBuf = source_path
.components()
.map(|component| {
if component.as_os_str() == "input" {
OsStr::new("expected")
} else {
component.as_os_str()
}
})
.collect();

let target = match std::fs::read_to_string(&target_path) {
Ok(t) => t,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
std::fs::write(target_path, fmt_text.clone()).unwrap();
fmt_text.clone()
}
Err(err) => unreachable!("{err}"),
};

// FIXME: better diff
assert_eq!(fmt_text, target);
}
}
}
5 changes: 5 additions & 0 deletions tooling/nargo_fmt/tests/execute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[cfg(test)]
mod tests {
// include tests generated by `build.rs`
include!(concat!(env!("OUT_DIR"), "/execute.rs"));
}
2 changes: 1 addition & 1 deletion tooling/noir_js_types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"The Noir Team <team@noir-lang.org>"
],
"packageManager": "yarn@3.5.1",
"version": "0.14.1",
"version": "0.16.0",
"license": "(MIT OR Apache-2.0)",
"files": [
"lib",
Expand Down

0 comments on commit e7f8f58

Please sign in to comment.