Skip to content

Commit 4927381

Browse files
committed
Add tests for license and style cmds
1 parent 9910b69 commit 4927381

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2022-2025 The Ramble Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
# option. This file may not be copied, modified, or distributed
7+
# except according to those terms.
8+
9+
import os
10+
11+
from ramble import main
12+
from ramble.cmd import license
13+
14+
license_cmd = main.RambleCommand("license")
15+
16+
17+
def test_verify_empty():
18+
out = license_cmd("verify", "--modified")
19+
assert "No license issues found" in out
20+
21+
22+
def test_verify_with_error(tmpdir):
23+
wrong_lic_header = """
24+
# Copyright 2020-2023 The Ramble Authors
25+
#
26+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
27+
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
28+
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
29+
# option. This file may not be copied, modified, or distributed
30+
# except according to those terms.
31+
"""
32+
with tmpdir.as_cwd():
33+
# Mimic the bin/
34+
os.mkdir("bin")
35+
file_with_lic = tmpdir / "bin" / "ramble"
36+
with open(file_with_lic, "w") as f:
37+
f.write(wrong_lic_header)
38+
out = license_cmd("verify", "--root", str(tmpdir), fail_on_error=False)
39+
assert "the license does not match the expected format" in out
40+
41+
new_header = wrong_lic_header.replace("2020-2023", license.strict_date_range)
42+
with open(file_with_lic, "w") as f:
43+
f.write(new_header)
44+
out = license_cmd("verify", "--root", str(tmpdir))
45+
assert "No license issues found" in out
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2022-2025 The Ramble Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
# option. This file may not be copied, modified, or distributed
7+
# except according to those terms.
8+
9+
import pytest
10+
11+
from ramble import main
12+
from ramble.cmd import style
13+
14+
style_cmd = main.RambleCommand("style")
15+
16+
17+
@pytest.mark.parametrize("tool", style.tool_names)
18+
def test_style(tool):
19+
out = style_cmd("--tool", tool, __file__)
20+
assert f"{tool} checks were clean" in out
21+
22+
23+
@pytest.mark.parametrize(
24+
"content,expected_err",
25+
[
26+
("import b\nimport a", "Imports are incorrectly sorted"),
27+
("import a\nimport b", "No newline at end of file"),
28+
],
29+
)
30+
def test_style_with_error(tmpdir, content, expected_err):
31+
with tmpdir.as_cwd():
32+
new_file = "new_file.py"
33+
with open(new_file, "w+") as f:
34+
f.write(content)
35+
36+
out = style_cmd(new_file, fail_on_error=False)
37+
assert style_cmd.returncode != 0
38+
assert expected_err in out
39+
40+
41+
def test_changed_files_all():
42+
files = style.changed_files(all_files=True)
43+
# Currently there are more than 900 files checked for styling.
44+
# Use a smaller number here.
45+
assert len(files) > 500
46+
47+
48+
def test_skip_tools():
49+
output = style_cmd("--skip", ",".join(style.tool_names))
50+
assert "Nothing to run" in output

0 commit comments

Comments
 (0)