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

Support source-directories from elm.json #28

Closed
wants to merge 5 commits into from
Closed

Support source-directories from elm.json #28

wants to merge 5 commits into from

Conversation

Janiczek
Copy link

@Janiczek Janiczek commented Feb 21, 2020

Connected to zwilias/elm-instrument#1

This is very very crude, solves the problem in a wrong way, creates more problems just to band-aid them later, but it unblocked me in my usecase.

The solved problem:
elm-coverage only reads src/ directory for sources

The solution:
elm-coverage now reads the elm.json file for source directories and threads that filepath info throughout its execution

It would be best to ditch this particular solution (it's really a hot pile of mess) and instead refactor the code to not assume some things (single source directory), but I'm offering this in case it helps anybody :)

@@ -52,10 +52,10 @@ var globalOptions = yargs =>
yargs
.positional("path", {
describe:
"Where are your sources located? This path will be " +
"Where is your elm.json located? This path will be " +
Copy link
Author

Choose a reason for hiding this comment

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

This argument changes meaning

@@ -73,10 +77,11 @@ var setupTests = (log, args) => () => {
"modifyingTests",
"Generating elm.json for coverage at " + tmpElmJson + "..."
);
var covSrc = path.resolve(path.join(coverageDir, args.path));
var originalPath = path.resolve(args.path);
Copy link
Author

Choose a reason for hiding this comment

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

These two variables were unused

return sourceDirectories(args)
.then(sourceDirs => Promise.all(sourceDirs.map(sourceDir => allSourcesFromDir(sourceDir))))
.then(sourcesArrays => sourcesArrays.flat());
};
Copy link
Author

Choose a reason for hiding this comment

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

This is the new machinery to read all files from all source-directories.
Potential problem: ES6 Array.flat usage in an otherwise quite conservative JavaScript

var elmJsonPath = path.join(args.path, 'elm.json');
return fs.readJson(elmJsonPath)
.then(elmJson => elmJson['source-directories']);
};
Copy link
Author

Choose a reason for hiding this comment

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

Reading the elm.json file here

return path.startsWith('..')
? path.replace(/^\.\./,'__dotdot')
: path;
}
Copy link
Author

Choose a reason for hiding this comment

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

Our code (that I'm hacking elm-coverage for) has this directory structure:

├── settings
│   ├── elm.json
│   ├── elm-stuff
│   ├── src
│   └── tests
├── _share
│   ├── benchmarks
│   ├── elm.json
│   ├── elm-stuff
│   ├── src
│   └── tests
├── tv-study
│   ├── elm.json
│   ├── elm-stuff
│   ├── src
│   └── tests

and some of these elm.jsons depend on each other, so we have:

    "source-directories": [
        "../_factories",
        "../_share/src",
        "src"
    ],

It's because of this case that I'm replacing .. with __dotdot. If we wanted to blindly copy the paths otherwise, it would leak out of the .coverage/instrumented folder. This way it stays inside, like .coverage/instrumented/__dotdot/_factories/Foo.elm

As the TODO comment says, it doesn't solve this very comprehensively (but it works for my case, so I stopped. Sorry :) Again, I don't think this PR should be accepted, maybe think of it just as a "lessons learned" point for if you ever want to do this properly)


elmPackage["name"] = "author/project";
elmPackage["source-directories"] = elmPackage["source-directories"]
.map(sourceDir => fixOutOfRootPath(sourceDir));
elmPackage["source-directories"].push("elm-coverage-src");
Copy link
Author

Choose a reason for hiding this comment

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

This is a bit less likely to collide. (Instead of src/Coverage.elm the user would have to have a source directory elm-coverage-src and inside Coverage.elm.)

@@ -227,7 +254,7 @@ var generateReport = (log, args) => () => {
return Promise.resolve();
case "human":
log.info("generating", "Generating report...");
return analyzer(args.path, data);
return analyzer(data);
Copy link
Author

Choose a reason for hiding this comment

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

Unused arg

module.exports = function(sourcePath) {
var revertOutOfRootFix = (path) => {
return path.replace(/__dotdot/g,'..');
}
Copy link
Author

Choose a reason for hiding this comment

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

This is the part where we try to translate from __dotdot back to ... We can't do it too quick because of the path.split('.') inside the thing that later tries to translate Foo.Bar back to Foo/Bar.elm

});

return {
coverageData: coverageData,
moduleMap: moduleMap
moduleMap: moduleMap,
presentablePathMap: presentablePathMap,
Copy link
Author

Choose a reason for hiding this comment

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

And so we do the translation in a new dictionary and pass that on to the CLI table reporter and the Elm app.

@zwilias zwilias deleted the branch zwilias:master January 8, 2022 14:52
@zwilias zwilias closed this Jan 8, 2022
@Janiczek
Copy link
Author

I think this was closed automatically by GitHub due to master->main branch renaming, but still would benefit from being open (see some comments in issue #44)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants