def test_3(self): module1 = Module(code_samples['3'].source_code) module2 = Module(code_samples['3b'].source_code) assert len(module1.blocks) == len(module2.blocks) == 2 assert module1.blocks[0] != module2.blocks[0] assert module1.blocks[1] == module2.blocks[1]
def test_classes(self): module1 = Module(code_samples['classes'].source_code) module2 = Module(code_samples['classes_b'].source_code) assert len(module1.blocks) == len(module2.blocks) == 3 assert module1.blocks[0] == module2.blocks[0] assert module1.blocks[1] != module2.blocks[1] assert module1.blocks[2] == module2.blocks[2]
def test_classes_header(self): module1 = Module(code_samples['classes'].source_code) module2 = Module(code_samples['classes_c'].source_code) assert len(module1.blocks) == len(module2.blocks) == 3 b1 = module1.blocks[0] b2 = module2.blocks[0] assert (b1.start, b1.end, b1.checksum) == (b2.start, b2.end, b2.checksum) assert (b1.name) != (b2.name) assert module1.blocks[1] == module2.blocks[1] assert module1.blocks[2] != module2.blocks[2]
def test_decorator(self): m = Module("""\ @abs def f1(): pass """) assert m.special_blocks == {3: 3}
def test_nonfunc_class(self, testdir, monkeypatch): """" """ cs1 = CodeSample("""\ class TestA(object): def test_one(self): print("1") def test_two(self): print("2") """) cs2 = CodeSample("""\ class TestA(object): def test_one(self): print("1") def test_twob(self): print("2") """) Module(cs2.source_code) test_a = testdir.makepyfile(test_a=cs1.source_code) result = testdir.runpytest("--testmon", "test_a.py::TestA::test_one", ) result.stdout.fnmatch_lines([ "*1 passed*", ]) testdir.makepyfile(test_a=cs2.source_code) test_a.setmtime(1424880935) result = testdir.runpytest("-v", "--collectonly", "--testmon") result.stdout.fnmatch_lines([ "*test_one*", ])
def test_create_nonempty_lines(self): 1 m = Module("""\ 1 2 """) assert m.full_lines == ["1", "2"]
def test_check_mtime(self): fs = SourceTree("") fs.cache["test_a.py"] = Module("", file_name="test_a.py", mtime=1, checksum=1000) assert check_mtime(fs, {"file_name": "test_a.py", "mtime": 1}) assert not check_mtime(fs, {"file_name": "test_a.py", "mtime": 2}) pytest.raises(Exception, check_mtime, fs, ("test_a.py", ))
def test_mtime_filter(self): fs = SourceTree("") fs.cache["test_a.py"] = Module("", file_name="test_a.py", mtime=1, checksum=1000) record = {"file_name": "test_a.py", "mtime": 1} assert split_filter(fs, check_mtime, (record, )) == ([record], []) record2 = {"file_name": "test_a.py", "mtime": 2} assert split_filter(fs, check_mtime, (record2, )) == ([], [record2])
def test_track_pytest_equal(self, testdir, monkeypatch): a = testdir.makepyfile(test_a="""\ def test_1(): a=1 """) def func(): testdir.runpytest("test_a.py") deps = track_it(testdir, func) assert {os.path.relpath(a.strpath, testdir.tmpdir.strpath): checksum_coverage(Module(file_name=a.strpath).blocks, [2])} == deps
def test_testmon_recursive(self, testdir, monkeypatch): a = testdir.makepyfile(test_a="""\ def test_1(): a=1 """) def func(): testdir.runpytest("test_a.py", "--testmon", "--capture=no") deps = track_it(testdir, func) # os.environ.pop('COVERAGE_TEST_TRACER', None) assert {os.path.abspath(a.strpath): checksum_coverage(Module(file_name=a.strpath).blocks, [2])} == deps
def test_subprocesss(testdir, monkeypatch): monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) a = testdir.makepyfile(test_a="""\ def test_1(): a=1 """) def func(): testdir.runpytest("test_a.py") deps = track_it(testdir, func) assert {os.path.relpath(a.strpath, testdir.tmpdir.strpath): checksum_coverage(Module(file_name=a.strpath).blocks, [2])} == deps
def test_classes_depggraph(self): module1 = Module(CodeSample("""\ class TestA(object): def test_one(self): print("1") def test_two(self): print("2") """).source_code) bs1 = module1.blocks module2 = Module(CodeSample("""\ class TestA(object): def test_one(self): print("1") def test_twob(self): print("2") """).source_code) bs2 = module2.blocks assert bs1[0] == bs2[0] assert bs1[1] != bs2[1] assert bs1[2] != bs2[2] assert len(module1.blocks) == len(module2.blocks) == 3 assert (bs1[1].start, bs1[1].end, bs1[1].checksum) == (bs2[1].start, bs2[1].end, bs2[1].checksum) assert (bs1[1].name) != (bs2[1].name) assert is_dependent({'test_s.py': [bs1[0].checksum, bs1[2].checksum]}, {'test_s.py': [b.checksum for b in bs2]}) == True assert is_dependent({'test_s.py': [bs1[1].checksum, bs1[2].checksum]}, {'test_s.py': [b.checksum for b in bs2]}) == True
def check_construct_fingerprints(self, text, lines=None, fingerprints=None): text = textwrap.dedent(text) coverage_lines, _ = self.write_and_run(text) m = Module(source_code=text) assert sorted(coverage_lines) == lines if fingerprints: assert create_fingerprints(m.lines, m.special_blocks, lines) == fingerprints
def test_check_checksum(self): fs = SourceTree("") fs.cache["test_a.py"] = Module("", file_name="test_a.py", mtime=1, checksum=1000) assert check_checksum(fs, {"file_name": "test_a.py", "checksum": 1000}) assert check_checksum(fs, { "file_name": "test_a.py", "checksum": 1001 }) is False assert pytest.raises(Exception, check_checksum, fs, { "file_name": "test_a.py", "bla": None })
def test_collect_garbage(self, tmdata): tmdata.write("test_1", {"test_1.py": encode_lines(["FINGERPRINT1"])}) tmdata.source_tree.cache["test_1.py"] = Module(source_code="") tmdata.source_tree.cache["test_1.py"].mtime = 1100.0 tmdata.source_tree.cache["test_1.py"].checksum = 600 tmdata.source_tree.cache["test_1.py"].fingerprint = "FINGERPRINT2" tmdata.determine_stable() assert set(tmdata.all_nodes) tmdata.sync_db_fs_nodes(retain=set()) tmdata.close_connection() td2 = CoreTestmonData("") td2.determine_stable() assert set(td2.all_nodes) == set()
def test_subprocess_recursive(testdir, monkeypatch): monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) # os.environ['COVERAGE_TEST_TRACER']='py' a = testdir.makepyfile(test_a="""\ def test_1(): a=1 """) def func(): testdir.runpytest("test_a.py", "--testmon", "--capture=no") deps = track_it(testdir, func) # os.environ.pop('COVERAGE_TEST_TRACER', None) assert {os.path.abspath(a.strpath): checksum_coverage(Module(file_name=a.strpath).blocks, [2])} == deps
def test_easy_by_block(self, testdir, monkeypatch): monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", 1) test_a = """ def test_add(): assert add(1, 2) == 3 def add(a, b): return a + b """ testdir.makepyfile(test_a=test_a) Module(source_code=test_a, file_name='test_a') result = testdir.runpytest("--testmon", "--tb=long", "-v") result.stdout.fnmatch_lines([ "*test_a.py::test_add PASSED*", ])
def get_file(self, filename): if filename not in self.cache: code, checksum = read_file_with_checksum( os.path.join(self.rootdir, filename) ) if checksum: fs_mtime = os.path.getmtime(os.path.join(self.rootdir, filename)) self.cache[filename] = Module( source_code=code, file_name=filename, rootdir=self.rootdir, mtime=fs_mtime, checksum=checksum, ) else: self.cache[filename] = None return self.cache[filename]
def check_human_coverage(self, text, lines=None, fingerprints=None): text = textwrap.dedent(text) coverage_lines, _ = self.write_and_run(text) m = Module(source_code=text) parser = PythonParser(text=text) parser.parse_source() statements = parser.statements executed = coverage_lines executed = parser.translate_lines(executed) hc = sorted( human_coverage(text, sorted(statements), sorted(statements - executed))) assert hc == lines if fingerprints: assert create_fingerprints(m.lines, m.special_blocks, lines) == fingerprints
def test_module_with_1250(self): Module(None, self.prepend_samples_dir("print1250r.py"))
def create_fingerprint_helper(afile, coverage): module = Module("\n".join(afile)) return create_fingerprints(module.lines, module.special_blocks, coverage)
if obj is None: return self value = obj.__dict__[self.func.__name__] = self.func(obj) return value def _get_python_lib_paths(): res = [sys.prefix] for attr in ["exec_prefix", "real_prefix", "base_prefix"]: if getattr(sys, attr, sys.prefix) not in res: res.append(getattr(sys, attr)) return [os.path.join(d, "*") for d in res] DISAPPEARED_FILE = Module("#dissapeared file") DISAPPEARED_FILE_CHECKSUM = 3948583 def home_file(node_name): return node_name.split("::", 1)[0] def is_python_file(file_path): return file_path[-3:] == ".py" def get_measured_relfiles(rootdir, cov, test_file): files = { test_file: set() }
def parse(source_code, file_name='a.py'): return Module(source_code=source_code, file_name=file_name).blocks
def checksum(code_sample): module = Module(code_sample.source_code, 'a.py') covdata = code_sample.expected_coverage return checksum_coverage(module.blocks, covdata)
def test_module_with_1250(): code_repr = Module(None, 'test/samples/print1250r.py').blocks[0].code assert "Str('\\xc5\\xa1')" in code_repr or "Str('ลก')" in Module(None, 'test/samples/print1250r.py').blocks[0].code
def parse_file(filename, rootdir, source_code): return Module(source_code=source_code, file_name=filename, rootdir=rootdir)
def parse_cache(self, module, new_mtime=None): if module not in self.modules_cache: self.modules_cache[module] = Module(file_name=module) self.mtimes[module] = new_mtime if new_mtime else os.path.getmtime(module) return self.modules_cache[module]