-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1983 from fabianegli/pre-commit-run-prettier
Use `pre-commit run prettier` if `prettier` is not available
- Loading branch information
Showing
5 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
black | ||
isort | ||
myst_parser | ||
pre-commit | ||
pytest-cov | ||
pytest-datafiles | ||
requests-mock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import shutil | ||
|
||
import git | ||
import pytest | ||
|
||
import nf_core.lint_utils | ||
|
||
JSON_WITH_SYNTAX_ERROR = "{'a':1, 1}" | ||
JSON_MALFORMED = "{'a':1}" | ||
JSON_FORMATTED = '{ "a": 1 }\n' | ||
|
||
WHICH_PRE_COMMIT = shutil.which("pre-commit") | ||
|
||
|
||
@pytest.fixture() | ||
def temp_git_repo(tmp_path_factory): | ||
tmp_git_dir = tmp_path_factory.mktemp("tmp_git_dir") | ||
repo = git.Repo.init(tmp_git_dir) | ||
return tmp_git_dir, repo | ||
|
||
|
||
@pytest.fixture(name="formatted_json") | ||
def git_dir_with_json(temp_git_repo): | ||
tmp_git_dir, repo = temp_git_repo | ||
file = tmp_git_dir / "formatted.json" | ||
with open(file, "w", encoding="utf-8") as f: | ||
f.write(JSON_FORMATTED) | ||
repo.git.add(file) | ||
return file | ||
|
||
|
||
@pytest.fixture(name="malformed_json") | ||
def git_dir_with_json_malformed(temp_git_repo): | ||
tmp_git_dir, repo = temp_git_repo | ||
file = tmp_git_dir / "malformed.json" | ||
with open(file, "w", encoding="utf-8") as f: | ||
f.write(JSON_MALFORMED) | ||
repo.git.add(file) | ||
return file | ||
|
||
|
||
@pytest.fixture(name="synthax_error_json") | ||
def git_dir_with_json_syntax_error(temp_git_repo): | ||
tmp_git_dir, repo = temp_git_repo | ||
file = tmp_git_dir / "synthax-error.json" | ||
with open(file, "w", encoding="utf-8") as f: | ||
f.write(JSON_WITH_SYNTAX_ERROR) | ||
repo.git.add(file) | ||
return file | ||
|
||
|
||
def test_run_prettier_on_formatted_file(formatted_json): | ||
nf_core.lint_utils.run_prettier_on_file(formatted_json) | ||
assert formatted_json.read_text() == JSON_FORMATTED | ||
|
||
|
||
def test_run_prettier_on_malformed_file(malformed_json): | ||
nf_core.lint_utils.run_prettier_on_file(malformed_json) | ||
assert malformed_json.read_text() == JSON_FORMATTED | ||
|
||
|
||
def test_run_prettier_on_synthax_error_file(synthax_error_json): | ||
with pytest.raises(ValueError) as exc_info: | ||
nf_core.lint_utils.run_prettier_on_file(synthax_error_json) | ||
assert exc_info.value.args[0].startswith(f"Can't format {synthax_error_json} because it has a synthax error.") |