Skip to content

Commit

Permalink
fix: added prettierrc and fixed prettier command
Browse files Browse the repository at this point in the history
  • Loading branch information
hritikgupta authored and loganek committed Jan 20, 2023
1 parent 9b97bfb commit 70fc310
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 131 deletions.
4 changes: 4 additions & 0 deletions tests/assemblyscript/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 4,
"printWidth": 120
}
106 changes: 50 additions & 56 deletions tests/assemblyscript/buildscripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,73 @@
import * as process from "process";
import { cpus } from "os";
import {
doesFileHaveExtension,
execAsPromise,
getFileModifiedTimestamp,
getPathsInDirectory,
mapFileExtension,
doesFileHaveExtension,
execAsPromise,
getFileModifiedTimestamp,
getPathsInDirectory,
mapFileExtension,
} from "./utility";
import Bottleneck from "bottleneck";

async function compileWithAsc(inputFilePath: string, outputFilePath: string) {
console.log(`Compiling ${inputFilePath}`);
return execAsPromise(
`npm run asc --silent -- "${inputFilePath}" -o "${outputFilePath}"`
);
console.log(`Compiling ${inputFilePath}`);
return execAsPromise(`npm run asc --silent -- "${inputFilePath}" -o "${outputFilePath}"`);
}

function isBinaryOutdated(srcPath: string, wasmPath: string): boolean {
const sourceTs = getFileModifiedTimestamp(srcPath);
if (!sourceTs) {
throw new Error(`Source file ${srcPath} doesn't exist`);
}
const wasmTs = getFileModifiedTimestamp(wasmPath);
const sourceTs = getFileModifiedTimestamp(srcPath);
if (!sourceTs) {
throw new Error(`Source file ${srcPath} doesn't exist`);
}
const wasmTs = getFileModifiedTimestamp(wasmPath);

return !wasmTs || sourceTs > wasmTs;
return !wasmTs || sourceTs > wasmTs;
}

async function compileTests() {
const testSuiteFiles = getPathsInDirectory("testsuite");
const pendingCompilations = testSuiteFiles
.filter((filePath) => doesFileHaveExtension(filePath, "ts"))
.map((filePath) => ({
src: filePath,
wasm: mapFileExtension(filePath, "ts", "wasm"),
}))
.filter((files) => isBinaryOutdated(files.src, files.wasm))
.map((files) =>
compileWithAsc(files.src, files.wasm)
.then((_) => undefined)
.catch((error) => ({ source: files.src, error }))
);
const testSuiteFiles = getPathsInDirectory("testsuite");
const pendingCompilations = testSuiteFiles
.filter((filePath) => doesFileHaveExtension(filePath, "ts"))
.map((filePath) => ({
src: filePath,
wasm: mapFileExtension(filePath, "ts", "wasm"),
}))
.filter((files) => isBinaryOutdated(files.src, files.wasm))
.map((files) =>
compileWithAsc(files.src, files.wasm)
.then((_) => undefined)
.catch((error) => ({ source: files.src, error }))
);

const errors = (await Promise.allSettled(pendingCompilations))
.map((p) => {
if (p.status === "fulfilled" && p.value !== undefined) {
return `Failed to compile ${p.value.source}:\n${p.value.error}`;
} else if (p.status === "rejected") {
return `Execution failed:\n${p.reason}`;
} else {
return undefined;
}
})
.filter((p) => p !== undefined) as string[];
const errors = (await Promise.allSettled(pendingCompilations))
.map((p) => {
if (p.status === "fulfilled" && p.value !== undefined) {
return `Failed to compile ${p.value.source}:\n${p.value.error}`;
} else if (p.status === "rejected") {
return `Execution failed:\n${p.reason}`;
} else {
return undefined;
}
})
.filter((p) => p !== undefined) as string[];

if (errors.length > 0) {
throw new Error(errors.join("\n\n"));
}
if (errors.length > 0) {
throw new Error(errors.join("\n\n"));
}
}

const limiter = new Bottleneck({
maxConcurrent: process.env.npm_config_concurrency
? parseInt(process.env.npm_config_concurrency)
: cpus().length,
minTime: process.env.npm_config_mintime
? parseInt(process.env.npm_config_mintime)
: 500,
maxConcurrent: process.env.npm_config_concurrency ? parseInt(process.env.npm_config_concurrency) : cpus().length,
minTime: process.env.npm_config_mintime ? parseInt(process.env.npm_config_mintime) : 500,
});

const wrapped = limiter.wrap(compileTests);
wrapped()
.then(() => {
console.log("Tests compiled");
})
.catch((e) => {
console.error(`Tests failed to compile:`);
console.error(e);
process.exit(1);
});
.then(() => {
console.log("Tests compiled");
})
.catch((e) => {
console.error(`Tests failed to compile:`);
console.error(e);
process.exit(1);
});
38 changes: 13 additions & 25 deletions tests/assemblyscript/buildscripts/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,28 @@ import * as fs from "fs";
import * as path from "path";

export async function execAsPromise(command: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
childProcess.exec(command, (error) => {
error ? reject(error) : resolve();
return new Promise<void>((resolve, reject) => {
childProcess.exec(command, (error) => {
error ? reject(error) : resolve();
});
});
});
}

export function getFileModifiedTimestamp(filePath: string): number | undefined {
if (!fs.existsSync(filePath)) {
return undefined;
}
return fs.statSync(filePath).mtime.getTime();
if (!fs.existsSync(filePath)) {
return undefined;
}
return fs.statSync(filePath).mtime.getTime();
}

export function getPathsInDirectory(directory: string): string[] {
return fs
.readdirSync(directory)
.map((filepath) => path.join(directory, filepath));
return fs.readdirSync(directory).map((filepath) => path.join(directory, filepath));
}

export function doesFileHaveExtension(
filepath: string,
extension: string
): boolean {
return filepath.endsWith(`.${extension}`);
export function doesFileHaveExtension(filepath: string, extension: string): boolean {
return filepath.endsWith(`.${extension}`);
}

export function mapFileExtension(
filepath: string,
oldExtension: string,
newExtension: string
): string {
return filepath.replace(
new RegExp(`(^.*)\\.${oldExtension}$`),
`$1.${newExtension}`
);
export function mapFileExtension(filepath: string, oldExtension: string, newExtension: string): string {
return filepath.replace(new RegExp(`(^.*)\\.${oldExtension}$`), `$1.${newExtension}`);
}
6 changes: 3 additions & 3 deletions tests/assemblyscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"asc": "asc",
"lint": "eslint . --ext .ts",
"build": "ts-node buildscripts/build.ts",
"prettier-format-check": "prettier '**/*.ts' --check",
"prettier-format-apply": "prettier '**/*.ts' --write"
"prettier-format-check": "npx prettier --config .prettierrc '**/*.ts' --check",
"prettier-format-apply": "npx prettier --config .prettierrc '**/*.ts' --write"
},
"repository": {
"type": "git",
Expand All @@ -28,7 +28,7 @@
"assemblyscript": "^0.22.0",
"bottleneck": "^2.19.5",
"eslint": "^8.26.0",
"prettier": "2.8.3",
"prettier": "^2.8.3",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
}
Expand Down
12 changes: 4 additions & 8 deletions tests/assemblyscript/testsuite/args_get-multiple-arguments.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
args_sizes_get,
args_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { args_sizes_get, args_get, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const dataBuf = memory.data(sizeof<usize>() * 2);

Expand All @@ -23,8 +19,8 @@ assert(err == errno.SUCCESS);
const expected = ["first", 'the "second" arg', "3"];

for (let i = 1; i < <i32>argCount; ++i) {
const ptr = load<usize>(argBuf + i * sizeof<usize>());
const str = String.UTF8.decodeUnsafe(ptr, ptr + argBufSize - argBuf, true);
assert(str == expected[i - 1]);
const ptr = load<usize>(argBuf + i * sizeof<usize>());
const str = String.UTF8.decodeUnsafe(ptr, ptr + argBufSize - argBuf, true);
assert(str == expected[i - 1]);
}
__free(argBuf);
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
args_sizes_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { args_sizes_get, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const buf = memory.data(sizeof<usize>());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
args_sizes_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { args_sizes_get, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const buf = memory.data(sizeof<usize>());

Expand Down
12 changes: 6 additions & 6 deletions tests/assemblyscript/testsuite/environ_get-multiple-variables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
environ_sizes_get,
environ_get,
errno,
environ_sizes_get,
environ_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const dataBuf = memory.data(sizeof<usize>() * 2);
Expand All @@ -23,8 +23,8 @@ assert(err == errno.SUCCESS);
const expected = ["a=text", 'b=escap " ing', "c=new\nline"];

for (let i = 0; i < <i32>envCount; ++i) {
const ptr = load<usize>(envBuf + i * sizeof<usize>());
const str = String.UTF8.decodeUnsafe(ptr, ptr + envBufSize - envBuf, true);
assert(str == expected[i]);
const ptr = load<usize>(envBuf + i * sizeof<usize>());
const str = String.UTF8.decodeUnsafe(ptr, ptr + envBufSize - envBuf, true);
assert(str == expected[i]);
}
__free(envBuf);
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
environ_sizes_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { environ_sizes_get, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const buf = memory.data(sizeof<usize>());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
environ_sizes_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { environ_sizes_get, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const buf = memory.data(sizeof<usize>());

Expand Down
5 changes: 1 addition & 4 deletions tests/assemblyscript/testsuite/fd_write-to-invalid-fd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
fd_write,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { fd_write, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const outSize = memory.data(sizeof<usize>());
const invalidFd = -31337;
Expand Down
6 changes: 1 addition & 5 deletions tests/assemblyscript/testsuite/fd_write-to-stdout.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
fd_write,
iovec,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { fd_write, iovec, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const message = "hello";

Expand Down
5 changes: 1 addition & 4 deletions tests/assemblyscript/testsuite/random_get-non-zero-length.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
random_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { random_get, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const bufSize = 32;
const buf = memory.data(bufSize);
Expand Down
5 changes: 1 addition & 4 deletions tests/assemblyscript/testsuite/random_get-zero-length.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
random_get,
errno,
} from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";
import { random_get, errno } from "@assemblyscript/wasi-shim/assembly/bindings/wasi_snapshot_preview1";

const bufSize = 0;
const buf = __alloc(bufSize);
Expand Down

0 comments on commit 70fc310

Please sign in to comment.