Skip to content

Commit

Permalink
add nix
Browse files Browse the repository at this point in the history
  • Loading branch information
stoeffel committed Oct 20, 2023
1 parent 47ae32d commit 4da8bf2
Show file tree
Hide file tree
Showing 10 changed files with 3,759 additions and 1,819 deletions.
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
npm-install:
npm install

build: npm-install
elm make src/VerifyExamples.elm --output bin/elm.js --optimize

verify-own-docs: build
./bin/cli.js

test: verify-own-docs
./run-tests.sh
run-examples: build
cd example && ../bin/cli.js

watch:
watchexec \
--clear \
--restart \
--watch src \
--watch bin \
--watch example/src \
--watch elm.json \
"make test"


release-major: test
npx xyz --repo git@github.com:stoeffel/elm-verify-examples.git --increment major

release-minor: test
npx xyz --repo git@github.com:stoeffel/elm-verify-examples.git --increment minor

release-patch: test
npx xyz --repo git@github.com:stoeffel/elm-verify-examples.git --increment patch
6 changes: 0 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,3 @@ $ # you can also pass arguments to elm-test with --elm-test-args
```

It will use the elm-test installed with this package.

## Examples

You can run the examples using:

`npm start`
5 changes: 3 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ var argv = require("yargs")
"elm-test-args",
'Pass arguments to elm-test. f.e. `--elm-test-args="--report=junit"`'
)
.coerce("elm-test-args", function(arg) {
.coerce("elm-test-args", function (arg) {
if (typeof arg === "string") return arg.split(" ");
return [];
})
.default("elm-test-args", []).argv;

var model = runner.init(argv);

runner.run(model, function(warnings) {
runner.run(model, function (warnings) {
console.log("warnings", warnings);
warnings.map(runner.warnModule(model));
if (model["run-tests"]) {
var status = runner.runElmTest(model);
Expand Down
89 changes: 46 additions & 43 deletions bin/runner.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// imports
var path = require("path");
var mkdirp = require("mkdirp");
var { mkdirp } = require("mkdirp");
var fs = require("fs");
var Elm = require("./elm.js");
var helpers = require("./cli-helpers.js");
var rimraf = require("rimraf");
var { rimrafSync } = require("rimraf");
var childProcess = require("child_process");
var chalk = require("chalk");

// loaders are called by init
var init = function(args) {
var init = function (args) {
var configJson = "elm-verify-examples.json";
var configPath = path.join(process.cwd(), args.output, configJson);
var verifyExamplesConfig = helpers.loadVerifyExamplesConfig(configPath);
verifyExamplesConfig.testsPath = path.join(process.cwd(), args.output);
var config = forFiles(verifyExamplesConfig, args._);

return Object.assign(args, config, {
testsDocPath: path.join(args.output, "VerifyExamples")
testsDocPath: path.join(args.output, "VerifyExamples"),
});
};

Expand All @@ -36,17 +36,17 @@ function generate(model, allTestsGenerated) {
}

var app = Elm.Elm.VerifyExamples.init({ flags: model });
app.ports.reportError.subscribe(function(err) {
app.ports.reportError.subscribe(function (err) {
console.error(err);
process.exit(1);
});

app.ports.readFile.subscribe(function(inputName) {
app.ports.readFile.subscribe(function (inputName) {
if (path.extname(inputName) === ".md") {
readSource(inputName, function(fileText) {
readSource(inputName, function (fileText) {
app.ports.generateMarkdownVerifyExamples.send({
fileName: cleanMarkdownPath(inputName),
fileText: fileText
fileText: fileText,
});
});
} else {
Expand All @@ -56,25 +56,25 @@ function generate(model, allTestsGenerated) {
elmModuleToPath(inputName)
);

readSource(pathToModule, function(fileText) {
readSource(pathToModule, function (fileText) {
app.ports.generateModuleVerifyExamples.send({
moduleName: inputName,
fileText: fileText,
ignoredWarnings: ignoredWarnings(model.ignoreWarnings, inputName)
ignoredWarnings: ignoredWarnings(model.ignoreWarnings, inputName),
});
});
}
});

var warnings = [];
app.ports.warn.subscribe(function(args) {
app.ports.warn.subscribe(function (args) {
if (args.warnings.length === 0) return;
warnings.push(args);
});

var writtenTests = 0;
app.ports.writeFiles.subscribe(function(data) {
serial(data, writeFile(model.testsDocPath), function() {
app.ports.writeFiles.subscribe(function (data) {
serial(data, writeFile(model.testsDocPath), function () {
writtenTests = writtenTests + 1;
if (writtenTests === model.tests.length && allTestsGenerated) {
allTestsGenerated(warnings);
Expand Down Expand Up @@ -106,16 +106,16 @@ function runElmTest(model) {
model.elmTestArgs.unshift(model.testsDocPath);
return childProcess.spawnSync(elmTest, model.elmTestArgs, {
cwd: process.cwd(),
stdio: "inherit"
stdio: "inherit",
}).status;
}

function cleanup(model) {
rimraf.sync(model.testsDocPath);
rimrafSync(model.testsDocPath);
}

function warnModule(model) {
return function(warnings) {
return function (warnings) {
if (!model.warn || warnings.warnings.length === 0) return;
warn(chalk.underline("Warnings in module " + warnings.moduleName));
warn("\n");
Expand All @@ -126,7 +126,7 @@ function warnModule(model) {

function warnSummary(model, warnings) {
if (!model.warn) return;
var count = warnings.reduce(function(acc, warning) {
var count = warnings.reduce(function (acc, warning) {
return warning.warnings.length + acc;
}, 0);
if (count > 0) {
Expand All @@ -151,7 +151,7 @@ function forFiles(model, files) {
}

model.tests = files
.filter(function(v) {
.filter(function (v) {
return v.endsWith(".elm");
})
.map(elmPathToModule(model.root, model.testsPath));
Expand All @@ -160,8 +160,8 @@ function forFiles(model, files) {
}

function serial(xs, f, done) {
var run = function(x, rest) {
f(x, function() {
var run = function (x, rest) {
f(x, function () {
if (rest.length > 0) {
run(rest[0], rest.slice(1));
} else {
Expand All @@ -173,7 +173,7 @@ function serial(xs, f, done) {
}

function writeFile(testsDocPath) {
return function(data, done) {
return function (data, done) {
var test = data.content;
var parts = data.moduleName.split(".");
var modulePath = [];
Expand All @@ -188,32 +188,35 @@ function writeFile(testsDocPath) {

var testsDocModulePath = path.join(testsDocPath, modulePath.join("/"));

mkdirp(testsDocModulePath, function(err) {
if (err) {
mkdirp(testsDocModulePath)
.then(function () {
fs.writeFile(
path.join(testsDocModulePath, moduleName + ".elm"),
test,
"utf8",
function (err) {
if (err) {
console.error(err);
process.exit(1);
return;
}

done();
}
);
})
.catch((err) => {
console.log(
chalk.red("Error creating directory: " + testsDocModulePath)
);
console.error(err);
process.exit(1);
return;
}
fs.writeFile(
path.join(testsDocModulePath, moduleName + ".elm"),
test,
"utf8",
function(err) {
if (err) {
console.error(err);
process.exit(1);
return;
}

done();
}
);
});
});
};
}

function elmPathToModule(root, testsPath) {
return function(pathName) {
return function (pathName) {
var relativePath = path.relative(
path.resolve(path.join(testsPath, root)),
pathName
Expand All @@ -230,7 +233,7 @@ function elmModuleToPath(moduleName) {
}

function readSource(filePath, onSuccess) {
fs.readFile(filePath, "utf8", function(err, fileText) {
fs.readFile(filePath, "utf8", function (err, fileText) {
if (err) {
console.error(err);
process.exit(1);
Expand All @@ -254,5 +257,5 @@ module.exports = {
cleanup: cleanup,
warnModule: warnModule,
warnSummary: warnSummary,
runElmTest: runElmTest
runElmTest: runElmTest,
};
13 changes: 6 additions & 7 deletions example/src/Failing.elm
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module Failing
exposing
( noExample
, nope
)
module Failing exposing
( noExample
, nope
)

{-| -}


{-|
nope --> 2
nope --> 3
-}
nope =
3
2


noExample =
Expand Down
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = import nixpkgs { inherit system; };
in {
devShells = {
default = pkgs.mkShell {
buildInputs = [
pkgs.elmPackages.elm
pkgs.gnumake
pkgs.nodejs-19_x
pkgs.watchexec
];
};
};
});
}
Loading

0 comments on commit 4da8bf2

Please sign in to comment.