Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
aspeddro committed Jan 22, 2024
1 parent 532af3a commit 1fca012
Show file tree
Hide file tree
Showing 12 changed files with 625 additions and 4 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/doc-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test Doc Examples

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js 18
uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm run test-doc-examples
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ lib/bs
.merlin
.bsb.lock
/node_modules/
.DS_Store
.DS_Store


test-compiler-examples/node_modules
test-compiler-examples/lib
test-compiler-examples/*.tgz
_tempFile.res
24 changes: 24 additions & 0 deletions bindings/Node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


var Path = {};

var $$URL = {};

var Process = {};

var Fs = {};

var $$Buffer = {};

var ChildProcess = {};

export {
Path ,
$$URL ,
Process ,
Fs ,
$$Buffer ,
ChildProcess ,
}
/* No side effect */
44 changes: 44 additions & 0 deletions bindings/Node.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Path = {
@module("path") external join2: (string, string) => string = "join"
@module("path") @variadic external join: array<string> => string = "join"
@module("path") external dirname: string => string = "dirname"
}

module URL = {
@module("url") external fileURLToPath: string => string = "fileURLToPath"
}

module Process = {
@scope("process") external exit: int => unit = "exit"
}

module Fs = {
@module("fs") external readFileSync: string => string = "readFileSync"
@module("fs") external readdirSync: string => array<string> = "readdirSync"
@module("fs") external writeFileSync: (string, string) => unit = "writeFileSync"
@module("fs") external existsSync: string => bool = "existsSync"

type statSync
@module("fs") external statSync: string => statSync = "statSync"

@send external isDirectory: statSync => bool = "isDirectory"
}

module Buffer = {
type t
@send external toString: t => string = "toString"
}

module ChildProcess = {
type execSyncOpts = {stdio?: string, cwd?: string}
@module("child_process")
external execFileSync: (string, array<string>, execSyncOpts) => Buffer.t = "execFileSync"

type spawnSyncReturns = {
status: Js.Null.t<int>,
stdout: Buffer.t,
stderr: Buffer.t,
}
@module("child_process")
external spawnSync: (string, array<string>) => spawnSyncReturns = "spawnSync"
}
8 changes: 8 additions & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
{
"dir": "test",
"type": "dev"
},
{
"dir": "scripts",
"type": "dev"
},
{
"dir": "bindings",
"type": "dev"
}
],
"suffix": ".mjs",
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"clean": "rescript clean",
"build": "rescript",
"watch": "rescript build -w",
"test": "node test/TestSuite.mjs && node test/TempTests.mjs"
"test": "node test/TestSuite.mjs && node test/TempTests.mjs",
"test-doc-examples": "node scripts/ExamplesTest.mjs"
},
"keywords": [
"rescript"
Expand All @@ -26,7 +27,7 @@
"rescript": "^10.1.0 || ^11.0.0-alpha.0 || next"
},
"devDependencies": {
"rescript": "10.1.4",
"@babel/code-frame": "7.18.6"
"@babel/code-frame": "7.18.6",
"rescript": "10.1.4"
}
}
252 changes: 252 additions & 0 deletions scripts/ExamplesTest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Fs from "fs";
import * as Url from "url";
import * as Path from "path";
import * as Belt_List from "rescript/lib/es6/belt_List.js";
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
import * as Belt_Result from "rescript/lib/es6/belt_Result.js";
import * as Core__Array from "../src/Core__Array.mjs";
import * as Child_process from "child_process";

var dirname = Path.dirname(Url.fileURLToPath(import.meta.url));

var tempFileName = "_tempFile.res";

var compilerDir = Path.join(Path.dirname(dirname), "test-compiler-examples");

var rescriptBin = Path.join(compilerDir, "node_modules", ".bin", "rescript");

var bscBin = Path.join(compilerDir, "node_modules", ".bin", "bsc");

var rescriptCoreCompiled = Path.join(compilerDir, "node_modules", "@rescript", "core", "lib", "ocaml");

function prepareCompiler(param) {
var corePath = Path.join(compilerDir, "..");
if (!Fs.existsSync(rescriptCoreCompiled)) {
console.log("Create a tarball from rescript core");
Child_process.execFileSync("npm", [
"pack",
corePath
], {
stdio: "ignore",
cwd: compilerDir
});
}
if (!Fs.existsSync(bscBin)) {
console.log("Installing core");
Child_process.execFileSync("npm", ["install"], {
cwd: compilerDir
});
}
if (!Fs.existsSync(rescriptCoreCompiled)) {
console.log("Running ReScript Build");
Child_process.execFileSync(rescriptBin, ["build"], {
cwd: compilerDir
});
return ;
}

}

prepareCompiler(undefined);

console.log("Running tests...");

function testCodeBlock(code) {
Fs.writeFileSync(tempFileName, code);
var args = [
tempFileName,
"-I",
rescriptCoreCompiled,
"-w",
"-109",
"-uncurried",
"-open",
"RescriptCore"
];
var spawn = Child_process.spawnSync(bscBin, args);
var status = spawn.status;
if (status !== null && status === 0) {
return {
TAG: /* Ok */0,
_0: undefined
};
} else {
return {
TAG: /* Error */1,
_0: spawn.stderr.toString()
};
}
}

function parseFile(file) {
var content = Fs.readFileSync(file).toString();
var lines = Belt_List.fromArray(content.split("\n"));
var getCodeblock = function (_lines, _result) {
while(true) {
var result = _result;
var lines = _lines;
if (lines) {
var rest = lines.tl;
var hd = lines.hd;
if (hd.trim().endsWith("```")) {
return [
rest,
Belt_Array.reverse(result)
];
}
_result = [hd].concat(result);
_lines = rest;
continue ;
}
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"ExamplesTest.res",
89,
16
],
Error: new Error()
};
};
};
var f = function (_lines, _result) {
while(true) {
var result = _result;
var lines = _lines;
if (!lines) {
return result;
}
var rest = lines.tl;
if (lines.hd.trim().startsWith("```res")) {
var match = getCodeblock(rest, []);
_result = {
hd: match[1],
tl: result
};
_lines = match[0];
continue ;
}
_lines = rest;
continue ;
};
};
var result = f(lines, /* [] */0);
return [
file,
Belt_List.toArray(result)
];
}

function isReScriptFile(file) {
if (file.endsWith(".res")) {
return true;
} else {
return file.endsWith(".resi");
}
}

function getAllReScriptFiles(dir) {
var loop = function (_acc, _result) {
while(true) {
var result = _result;
var acc = _acc;
if (!acc) {
return result;
}
var tail = acc.tl;
var hd = acc.hd;
if (Fs.statSync(Path.join(dir, hd)).isDirectory()) {
var path = Path.join(dir, hd);
var a = Belt_List.fromArray(Fs.readdirSync(path).map((function(hd){
return function (file) {
return Path.join(hd, file);
}
}(hd))));
_acc = Belt_List.concat(a, tail);
continue ;
}
_result = {
hd: hd,
tl: result
};
_acc = tail;
continue ;
};
};
return Belt_List.toArray(loop(Belt_List.fromArray(Fs.readdirSync(dir)), /* [] */0)).filter(isReScriptFile);
}

var files = Core__Array.reduce(getAllReScriptFiles("src"), [], (function (acc, file) {
if (acc.includes(file + "i")) {
return acc;
} else {
return acc.concat([file]);
}
}));

var docstrings = files.map(function (file) {
return parseFile(Path.join("src", file));
}).filter(function (param) {
return param[1].length > 0;
});

var tests = docstrings.map(function (param) {
var results = param[1].map(function (example) {
return testCodeBlock(example.join("\n"));
});
return [
param[0],
results
];
});

var someError = {
contents: false
};

tests.forEach(function (param) {
var file = param[0];
var match = Core__Array.reduce(param[1], [
[],
[]
], (function (acc, result) {
if (Belt_Result.isOk(result)) {
acc[0].push(result);
} else {
acc[1].push(result);
}
return acc;
}));
var err = match[1];
console.log("" + file + ": Sucesss: " + match[0].length.toString() + ". Failed: " + err.length.toString() + "");
err.forEach(function (result) {
if (result.TAG === /* Ok */0) {
return ;
}
someError.contents = true;
console.log("\n " + file);
console.log(result._0);
});
});

process.exit(someError.contents ? 1 : 0);

export {
dirname ,
tempFileName ,
compilerDir ,
rescriptBin ,
bscBin ,
rescriptCoreCompiled ,
prepareCompiler ,
testCodeBlock ,
parseFile ,
isReScriptFile ,
getAllReScriptFiles ,
files ,
docstrings ,
tests ,
}
/* dirname Not a pure module */
Loading

0 comments on commit 1fca012

Please sign in to comment.