Skip to content

Testing suite improvements #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/testing-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"dependencies": {
"@bitauth/libauth": "^3.1.0-next.2",
"cashc": "^0.11.0",
"cashscript": "^0.11.0",
"url-join": "^5.0.0"
"cashscript": "^0.11.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.14",
"jest": "^29.7.0",
"tsx": "^4.19.2",
"typescript": "^5.7.3"
"typescript": "^5.7.3",
"url-join": "^5.0.0"
}
}
51 changes: 46 additions & 5 deletions examples/testing-suite/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,58 @@ import fs from 'fs';
import { URL } from 'url';
import urlJoin from 'url-join';

interface CompilationCacheItem {
mtime: number;
}

export const compile = (): void => {
const directory = new URL('../contracts', import.meta.url);
const result = fs.readdirSync(directory)
const cacheDirectory = new URL('../cache', import.meta.url);
fs.mkdirSync(cacheDirectory, { recursive: true });
const cacheFile = new URL('../cache/cashc.json', import.meta.url);
let compilationCache: Record<string, CompilationCacheItem> = {};
if (fs.existsSync(cacheFile)) {
compilationCache = JSON.parse(fs.readFileSync(cacheFile, { encoding: 'utf-8' }));
}

const artifactsDirectory = new URL('../artifacts', import.meta.url);
fs.mkdirSync(artifactsDirectory, { recursive: true });

const contractsDirectory = new URL('../contracts', import.meta.url);
const result = fs.readdirSync(contractsDirectory)
.filter((fn) => fn.endsWith('.cash'))
.map((fn) => ({ fn, contents: fs.readFileSync(new URL(urlJoin(directory.toString(), fn)), { encoding: 'utf-8' }) }));
.map((fn) => ({ fn, contents: fs.readFileSync(new URL(urlJoin(contractsDirectory.toString(), fn)), { encoding: 'utf-8' }) }));

result.forEach(({ fn, contents }) => {
const artifact = compileString(contents);
const mtime = fs.statSync(new URL(urlJoin(contractsDirectory.toString(), fn))).mtimeMs;
if (!compilationCache[fn] || compilationCache[fn].mtime !== mtime) {
console.log(`Compiling ${fn}...`);
const artifact = compileString(contents);

fs.writeFileSync(new URL(`../artifacts/${fn.replace('.cash', '.json')}`, import.meta.url), JSON.stringify(artifact, null, 2));
exportArtifact(artifact, new URL(`../artifacts/${fn.replace('.cash', '.artifact.ts')}`, import.meta.url));
compilationCache[fn] = { mtime };
}
});

// Write the updated cache back to the file
fs.writeFileSync(cacheFile, JSON.stringify(compilationCache, null, 2), { encoding: 'utf-8' });
};

export const exportArtifact = (obj: object, outPath: string | URL): void => {
const toTs = (value: any): string => {
// First, stringify the object with indentation
let json = JSON.stringify(value, null, 2);

// Remove quotes from object keys where valid (simple JS identifiers)
json = json.replace(
/"([a-zA-Z_][a-zA-Z0-9_]*)":/g,
'$1:'
);

return json;
};

const content = `export default ${toTs(obj)} as const;`;
fs.writeFileSync(outPath, content, { encoding: 'utf-8' });
};

switch (process.argv[2]) {
Expand Down