Skip to content
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

Update tooling and and add nix #107

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- run: nix develop --command make test
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
npm-install:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking it's a good idea to declare these as .PHONY in a makefile, you can skip errors when a file called test is accidentally created your tests won't run.

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log("warnings", warnings);
console.warn("warnings", warnings);

AFAIK it goes to stderr in node, which is probably more desirable.

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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be easier to eventually rewrite this in async/await style, but that's perhaps for another PR...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!

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.

Loading