from __future__ import annotations
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import ships_nothing as filt
SHIPPED = '''pub fn width(cols: u16) -> u16 {
cols / 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn halves() {
assert_eq!(width(4), 2);
}
}
'''
def run(cwd, *args):
return subprocess.run(args, cwd=cwd, check=True, capture_output=True, text=True).stdout
class Repo:
def __init__(self, tmp: Path):
self.path = tmp
run(tmp, "git", "init", "-q", "-b", "main")
run(tmp, "git", "config", "user.email", "t@example.com")
run(tmp, "git", "config", "user.name", "t")
def commit(self, name: str, text: str):
(self.path / name).write_text(text)
run(self.path, "git", "add", "-A")
run(self.path, "git", "commit", "-q", "-m", "c")
def verdict(self, path="a.rs", base="main", head="HEAD"):
return filt.ships_nothing(base, head, path, cwd=self.path)
class Ranges(unittest.TestCase):
def test_a_cfg_test_module_is_found(self):
self.assertEqual(filt.cfg_test_ranges(SHIPPED), [(5, 13)])
def test_a_brace_in_a_string_does_not_end_the_block_early(self):
text = (
"fn f() {}\n"
"#[cfg(test)]\n"
"mod tests {\n"
' const S: &str = "}";\n'
" fn g() {}\n"
"}\n"
)
self.assertEqual(filt.cfg_test_ranges(text), [(2, 6)])
def test_a_cfg_test_item_that_is_not_a_module_is_not_a_range(self):
text = "fn f() {}\n#[cfg(test)]\nfn helper() -> u8 {\n 1\n}\n"
self.assertEqual(filt.cfg_test_ranges(text), [])
def test_a_file_with_no_tests_has_no_ranges(self):
self.assertEqual(filt.cfg_test_ranges("fn f() {}\n"), [])
class Verdicts(unittest.TestCase):
def test_a_change_inside_the_test_module_ships_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
repo.commit("b.rs", "fn b() {}\n")
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", SHIPPED.replace("width(4), 2", "width(6), 3"))
self.assertTrue(repo.verdict())
def test_a_change_outside_the_test_module_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", SHIPPED.replace("cols / 2", "cols / 3"))
self.assertFalse(repo.verdict())
def test_a_change_to_both_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit(
"a.rs",
SHIPPED.replace("cols / 2", "cols / 3").replace("width(4), 2", "width(6), 2"),
)
self.assertFalse(repo.verdict())
def test_deleting_a_test_ships_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", SHIPPED.replace(" assert_eq!(width(4), 2);\n", ""))
self.assertTrue(repo.verdict())
def test_deleting_shipped_code_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
deleted = "pub fn legacy() -> u16 {\n 1\n}\n\n"
repo.commit("a.rs", deleted + SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", SHIPPED)
self.assertFalse(repo.verdict())
def test_a_test_module_that_grows_ships_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit(
"a.rs",
SHIPPED.replace(
" #[test]\n",
" #[test]\n fn also() {\n assert_eq!(width(2), 1);\n }\n\n #[test]\n",
1,
),
)
self.assertTrue(repo.verdict())
def test_a_file_that_is_not_rust_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
page = (
"Testing conventions\n\n```rust\n#[cfg(test)]\nmod tests {\n"
" #[test]\n fn shown_to_the_reader() {}\n}\n```\n"
)
repo.commit("a.rs", SHIPPED)
repo.commit("guide.md", page)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("guide.md", page.replace("shown_to_the_reader", "renamed"))
self.assertFalse(repo.verdict(path="guide.md"))
def test_an_added_file_that_is_only_tests_ships_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit(
"c.rs",
"#[cfg(test)]\nmod tests {\n #[test]\n fn t() {}\n}\n",
)
self.assertTrue(repo.verdict(path="c.rs"))
def test_adding_the_cfg_test_attribute_to_a_shipped_module_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
shipped = "pub fn f() -> u8 {\n 1\n}\n\nmod helpers {\n pub fn g() -> u8 {\n 2\n }\n}\n"
repo.commit("a.rs", shipped)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", shipped.replace("\nmod helpers {", "\n#[cfg(test)]\nmod helpers {"))
self.assertFalse(repo.verdict())
def test_removing_the_cfg_test_attribute_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
gated = "pub fn f() -> u8 {\n 1\n}\n\n#[cfg(test)]\nmod helpers {\n pub fn g() -> u8 {\n 2\n }\n}\n"
repo.commit("a.rs", gated)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", gated.replace("#[cfg(test)]\n", ""))
self.assertFalse(repo.verdict())
def test_a_whole_test_module_added_at_once_still_ships_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", "pub fn f() -> u8 {\n 1\n}\n")
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit(
"a.rs",
"pub fn f() -> u8 {\n 1\n}\n\n#[cfg(test)]\nmod tests {\n"
" #[test]\n fn t() {\n assert_eq!(super::f(), 1);\n }\n}\n",
)
self.assertTrue(repo.verdict())
def test_a_module_left_open_at_end_of_file_is_not_a_range(self):
text = "fn f() {}\n#[cfg(test)]\nmod tests {\n #[test]\n fn t() {}\n"
self.assertEqual(filt.cfg_test_ranges(text), [])
def test_a_module_declaration_without_a_body_is_not_a_range(self):
text = "#[cfg(test)]\nmod tests;\npub fn shipped() -> u8 {\n 1\n}\n"
self.assertEqual(filt.cfg_test_ranges(text), [])
def test_a_doc_comment_between_the_attribute_and_its_module_is_read(self):
text = (
"fn f() {}\n#[cfg(test)]\n/// Unit tests for the parser.\n"
"mod tests {\n #[test]\n fn t() {}\n}\n"
)
self.assertEqual(filt.cfg_test_ranges(text), [(2, 7)])
def test_a_path_the_pathspec_cannot_resolve_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", SHIPPED.replace("width(4), 2", "width(6), 3"))
self.assertFalse(repo.verdict(path="does_not_exist.rs"))
def test_a_blank_line_inside_a_string_literal_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
before = 'pub const P: &str = r#"\ncroft\n\nv1\n"#;\n'
after = 'pub const P: &str = r#"\ncroft\nv1\n"#;\n'
repo.commit("a.rs", before)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", after)
self.assertFalse(repo.verdict(), "a blank line left a string literal")
def test_a_blank_line_added_to_a_string_literal_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
before = 'pub const P: &str = r#"\ncroft\nv1\n"#;\n'
after = 'pub const P: &str = r#"\ncroft\n\nv1\n"#;\n'
repo.commit("a.rs", before)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", after)
self.assertFalse(repo.verdict(), "a blank line joined a string literal")
def test_a_whole_test_module_in_its_real_shape_ships_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", "pub fn f() -> u8 {\n 1\n}\n")
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit(
"a.rs",
"pub fn f() -> u8 {\n 1\n}\n\n#[cfg(test)]\nmod tests {\n"
" use super::*;\n\n #[test]\n fn t() {\n"
" assert_eq!(f(), 1);\n }\n}\n",
)
self.assertTrue(repo.verdict(), "the module arrived in one piece")
def test_deleting_a_whole_test_module_in_its_real_shape_ships_nothing(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
whole = (
"pub fn f() -> u8 {\n 1\n}\n\n#[cfg(test)]\nmod tests {\n"
" use super::*;\n\n #[test]\n fn t() {\n"
" assert_eq!(f(), 1);\n }\n}\n"
)
repo = Repo(Path(tmp))
repo.commit("a.rs", whole)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", "pub fn f() -> u8 {\n 1\n}\n")
self.assertTrue(repo.verdict(), "and the module leaving in one piece")
def test_a_diff_git_reports_as_binary_ships(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
(Path(tmp) / "a.rs").write_bytes(b"pub fn f() -> u8 {\n 1\n}\n\x00\n")
run(repo.path, "git", "add", "-A")
run(repo.path, "git", "commit", "-q", "-m", "c")
run(repo.path, "git", "checkout", "-q", "-b", "work")
(Path(tmp) / "a.rs").write_bytes(b"pub fn f() -> u8 {\n 9\n}\n\x00\n")
run(repo.path, "git", "add", "-A")
run(repo.path, "git", "commit", "-q", "-m", "c2")
self.assertFalse(repo.verdict())
def test_a_mid_line_block_comment_yields_no_ranges(self):
text = (
"#[cfg(test)]\nmod tests {\n fn t() {\n let _x = 0; /* { */\n"
" }\n}\n\npub fn g() -> u8 {\n let _ = 0; /* } */\n 1\n}\n"
)
self.assertEqual(filt.cfg_test_ranges(text), [])
def test_the_cli_reports_the_verdict_as_its_exit_status(self):
with tempfile.TemporaryDirectory() as tmp:
repo = Repo(Path(tmp))
repo.commit("a.rs", SHIPPED)
run(repo.path, "git", "checkout", "-q", "-b", "work")
repo.commit("a.rs", SHIPPED.replace("width(4), 2", "width(6), 3"))
script = str(Path(__file__).resolve().parents[1] / "ships_nothing.py")
done = subprocess.run(
[sys.executable, script, "main", "HEAD", "a.rs"],
cwd=repo.path,
capture_output=True,
text=True,
)
self.assertEqual(done.returncode, 0, done.stderr)
repo.commit("a.rs", SHIPPED.replace("cols / 2", "cols / 3"))
ships = subprocess.run(
[sys.executable, script, "main", "HEAD", "a.rs"],
cwd=repo.path,
capture_output=True,
text=True,
)
self.assertEqual(ships.returncode, 1, ships.stderr)
if __name__ == "__main__":
unittest.main()