Skip to content

Commit

Permalink
Merge pull request #110 from stoeffel/glob
Browse files Browse the repository at this point in the history
no config
  • Loading branch information
stoeffel authored Jan 15, 2024
2 parents abc9ff1 + daeeaff commit 78b71b8
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 275 deletions.
24 changes: 22 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## 6.0.0

- Make `elm-verify-examples.json` optional [#110](https://github.com/stoeffel/elm-verify-examples/pull/110)

- Run elm-verify-examples on all elm files in source directory by default.

- Add option `all` to `elm-verify-examples.json` to verify all elm files.

```diff
+ "tests": "all"
```

- Remove `root` from elm-verify-examples.json. In addition it will respect source-directories defined in elm.json

To migrate, remove the "root" key in your config file and move it into `/tests`.

```diff
- "root": "../src",
```

## 5.3.0

- Do not run tests by default, only generate [#65](https://github.com/stoeffel/elm-verify-examples/issues/65)
- Add --run-tests (-r) option to run generated tests [#65](https://github.com/stoeffel/elm-verify-examples/issues/65)


## 2.0.0

- We have a changelog now :tada:
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: npm-install build test watch release-major release-minor release-patch
.PHONY: npm-install build test watch release-major release-minor release-patch run-examples verify-own-docs

npm-install:
npm install
Expand All @@ -11,8 +11,9 @@ verify-own-docs: build

test: verify-own-docs
./run-tests.sh

run-examples: build
cd example && ../bin/cli.js
cd example && ../bin/cli.js --run-tests

watch:
watchexec \
Expand Down
19 changes: 18 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ $ elm-test init

## Setup

There is no need for any configuration. elm-verify-examples will run on all elm files in the specified `source-directories` (`elm.json`). It's possible to create a config file if you want to run it on only a subset of files or on additional markdown files.

```bash
$ touch tests/elm-verify-examples.json
```
Expand All @@ -24,11 +26,26 @@ $ touch tests/elm-verify-examples.json

```json
{
"root": "../src",
"tests": ["Mock", "Mock.Foo.Bar.Moo", "./README.md"]
}
```

Alternatively you can run elm-verify-examples on all elm files in your source directories:

```json
{
"tests": "all"
}
```

or

```json
{
"tests": ["all", "Some.md"]
}
```

It's recommended to add `./tests/VerifyExamples` to your `.gitignore`.

If you are building a _package_, you can pass the string `"exposed"` instead of an explicit list of modules,
Expand Down
58 changes: 31 additions & 27 deletions bin/cli-helpers.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
var path = require("path");
var fsExtra = require("fs-extra");
const { globSync } = require("glob");

function loadVerifyExamplesConfig(configPath) {
/* load the doc test config if we can find it
otherwise, copy the template one and load that
*/

var verifyExamples = null;
var elmJson = null;

try {
verifyExamples = require(configPath);
if (verifyExamples["root"] !== undefined) {
console.warn(
"elm-verify-examples.json: 'root' is no longer a valid key. It defaults to point one directory up from `/tests`."
);
}
var elmJsonPath = findParentElmJson(path.dirname(configPath));
elmJson = require(elmJsonPath);
} catch (e) {
console.log(`Copying initial elm-verify-examples.json to ${configPath}`);
fsExtra.copySync(
path.resolve(__dirname, "./templates/elm-verify-examples.json"),
configPath
);

verifyExamples = require(path.resolve(
__dirname,
"templates/elm-verify-examples.json"
));
var elmJsonPath = findParentElmJson(path.dirname(configPath));
elmJson = require(elmJsonPath);
verifyExamples = { tests: "all" };
}

return resolveTests(configPath, verifyExamples);
return resolveTests(configPath, Object.assign({}, verifyExamples, elmJson));
}

function resolveTests(configPath, config) {
if (config.tests === "exposed") {
/* This is asserting that we want to run for all exposed modules in a package
*/
var elmJson = null;
var elmJsonPath = findParentElmJson(path.dirname(configPath));
try {
elmJson = require(elmJsonPath);
} catch (e) {
console.error(
"Config asks for 'exposed', but could not find elm.json at " +
elmJsonPath
);
process.exit(1);
}
if (elmJson.type == "package") {
config.tests = elmJson["exposed-modules"].concat("./README.md");
if (config.tests === "exposed" || config.tests.includes("exposed")) {
if (config.type == "package") {
config.tests = config["exposed-modules"].concat("./README.md");
} else {
console.error(
"Config asks for 'exposed', but elm.json type is not 'package'"
);
process.exit(1);
}
}
if (config.tests === "all" || config.tests.includes("all")) {
var allElmFiles = config["source-directories"]
.map((d) =>
globSync("**/*.elm", {
cwd: path.join(path.dirname(configPath), "..", d),
})
)
.flat()
.map(elmPathToModuleName);
config.tests = allElmFiles.concat("./README.md");
}
return config;
}

Expand All @@ -64,6 +64,10 @@ function findParentElmJson(p) {
}
}

function elmPathToModuleName(pathName) {
return pathName.slice(0, -4).replace(/\//g, ".");
}

module.exports = {
loadVerifyExamplesConfig: loadVerifyExamplesConfig,
};
38 changes: 28 additions & 10 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ function generate(model, allTestsGenerated) {
});
});
} else {
var pathToModule = path.join(
model.testsPath,
model.root,
var pathToModule = findModule(
path.join(model.testsPath, ".."),
model["source-directories"],
elmModuleToPath(inputName)
);

Expand All @@ -73,10 +73,17 @@ function generate(model, allTestsGenerated) {
});

var writtenTests = 0;
var noExamples = 0;
app.ports.noExamples.subscribe(function () {
noExamples = noExamples + 1;
});
app.ports.writeFiles.subscribe(function (data) {
serial(data, writeFile(model.testsDocPath), function () {
writtenTests = writtenTests + 1;
if (writtenTests === model.tests.length && allTestsGenerated) {
if (
writtenTests + noExamples === model.tests.length &&
allTestsGenerated
) {
allTestsGenerated(warnings);
}
});
Expand Down Expand Up @@ -154,7 +161,7 @@ function forFiles(model, files) {
.filter(function (v) {
return v.endsWith(".elm");
})
.map(elmPathToModule(model.root, model.testsPath));
.map(elmPathToModule("..", model["source-directories"], model.testsPath));

return model;
}
Expand Down Expand Up @@ -215,19 +222,30 @@ function writeFile(testsDocPath) {
};
}

function elmPathToModule(root, testsPath) {
function elmPathToModule(elmRoot, sourceDirs, testsPath) {
return function (pathName) {
var relativePath = path.relative(
path.resolve(path.join(testsPath, root)),
pathName
);
var relativePath = findModule(elmRoot, sourceDirs, pathName);
if (relativePath.startsWith("./")) {
relativePath = relativePath.substr(2);
}
return relativePath.substr(0, relativePath.length - 4).replace(/\//g, ".");
};
}

function findModule(elmRoot, sourceDirs, pathName) {
for (var i = 0; i < sourceDirs.length; i++) {
const dir = sourceDirs[i];
const module = path.join(elmRoot, dir, pathName);
if (fs.existsSync(module)) {
return module;
}
}
console.error(
`Could not find module ${pathName} in ${sourceDirs} with root ${elmRoot}`
);
process.exit(1);
}

function elmModuleToPath(moduleName) {
return moduleName.replace(/\./g, "/") + ".elm";
}
Expand Down
4 changes: 0 additions & 4 deletions bin/templates/elm-verify-examples.json

This file was deleted.

50 changes: 24 additions & 26 deletions example/elm.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"elm/browser": "1.0.0",
"elm/core": "1.0.0",
"elm/html": "1.0.0",
"elm/json": "1.0.0",
"elm/regex": "1.0.0",
"elm-community/list-extra": "8.0.0"
},
"indirect": {
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.0"
}
"type": "application",
"source-directories": ["src", "src-other/util"],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"elm/browser": "1.0.0",
"elm/core": "1.0.0",
"elm/html": "1.0.0",
"elm/json": "1.0.0",
"elm/regex": "1.0.0",
"elm-community/list-extra": "8.0.0"
},
"test-dependencies": {
"direct": {
"elm-explorations/test": "1.0.0"
},
"indirect": {
"elm/random": "1.0.0"
}
"indirect": {
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.0"
}
},
"test-dependencies": {
"direct": {
"elm-explorations/test": "1.0.0"
},
"indirect": {
"elm/random": "1.0.0"
}
}
}
13 changes: 13 additions & 0 deletions example/src-other/util/Other.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Other exposing (other)

{-| -}


{-| Stuff
other 4 --> 4
-}
other : Int -> Int
other x =
x + 1
27 changes: 0 additions & 27 deletions example/tests/elm-verify-examples.json

This file was deleted.

Loading

0 comments on commit 78b71b8

Please sign in to comment.