CLI improvements: support URL arguments, add tests, and fix minor bug #480
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change adds support for URLs (http/https) in the CLI arguments, besides filesystem paths, reducing the feature parity gap with the no longer maintained swagger-mock-validator lib (disclaimer: I was one of the co-authors of that library).
The CLI module did not have tests, so before introducing this change I added tests and refactored the implementation to make it testable.
Additionally I introduced a fix for a bug which, although very unlikely to be triggered, could have unwanted consequences: The cli command returns the number of pact files with errors as exit code, most systems will take only the least significant byte as exit code, i.e 0-255. If you happen to have, let's say 256 failed pacts, the exit code will overflow to 0. This number is now capped at 255.
The change was introduced gradually, in case you want to review commit by commit:
test: add integration tests for cli prior to refactoring: The current solution is not unit-testable, before refactoring it I've added two integration tests (one for a success case, one for a failed case)
refactor(cli): separate command parsing logic from execution: Moved the CLI execution logic (reading files, parsing them, invoking the comparator, printing output, collecting errors) into it's own module, separate from the commander's interface, following the single-responsibility principle. This allows testing the most part of the logic with fast & targeted unit tests. The module has full coverage now. It uses dependency inversion to mock the filesystem, stdout, and the comparator initialization.
fix: cap exit code at 255 to avoid overflowing to 0: adds test and fix the exit code bug capping it at 255.
feat(cli): support URLs for cli input arguments: detects whether an input argument is a url (http:// - https://) and fetches the content if so, otherwise defaults to the current implementation (interpreting the input as a filesystem path). Adds unit and integration tests for this feature.