-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
625 additions
and
4 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
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 |
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,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 */ |
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,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" | ||
} |
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
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,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 */ |
Oops, something went wrong.