Coverage for tests/test_plugin.py: 89%

35 statements  

« prev     ^ index     » next       coverage.py v7.3.3, created at 2024-01-02 20:32 +0100

1# SPDX-License-Identifier: MIT 

2# Copyright (c) 2023-2024 Kilian Lackhove 

3 

4import subprocess 

5from pathlib import Path 

6 

7import coverage 

8import pytest 

9 

10import coverage_sh 

11from coverage_sh import ShellPlugin 

12from coverage_sh.plugin import PatchedPopen 

13 

14 

15@pytest.fixture() 

16def examples_dir(resources_dir): 

17 return resources_dir / "examples" 

18 

19 

20def test_ShellPlugin_file_tracer(): 

21 assert False 

22 

23 

24def test_ShellPlugin_file_reporter(): 

25 assert False 

26 

27 

28def test_ShellPlugin_find_executable_files(examples_dir): 

29 plugin = ShellPlugin({}) 

30 

31 executable_files = plugin.find_executable_files(str(examples_dir)) 

32 

33 assert [Path(f) for f in sorted(executable_files)] == [ 

34 examples_dir / "shell-file.weird.suffix", 

35 ] 

36 

37 

38def test_patched_popen( 

39 resources_dir, 

40 dummy_project_dir, 

41 monkeypatch, 

42): 

43 monkeypatch.chdir(dummy_project_dir) 

44 

45 atexit_callables = [] 

46 

47 def atexit_register(callable_): 

48 atexit_callables.append(callable_) 

49 

50 monkeypatch.setattr(coverage_sh.plugin.atexit, "register", atexit_register) 

51 

52 cov = coverage.Coverage() 

53 cov.start() 

54 

55 test_sh_path = resources_dir / "testproject" / "test.sh" 

56 proc = PatchedPopen( 

57 ["/bin/bash", test_sh_path], 

58 stdout=subprocess.PIPE, 

59 stderr=subprocess.PIPE, 

60 encoding="utf8", 

61 ) 

62 proc.wait() 

63 

64 cov.stop() 

65 

66 assert proc.stderr.read() == "" 

67 assert proc.stdout.read() == ( 

68 "Hello, World!\n" 

69 "Variable is set to 'Hello, World!'\n" 

70 "Iteration 1\n" 

71 "Iteration 2\n" 

72 "Iteration 3\n" 

73 "Iteration 4\n" 

74 "Iteration 5\n" 

75 "Hello from a function!\n" 

76 "Current date is: Di 19. Jan 04:14:07 CET 2038\n" 

77 "5 + 3 = 8\n" 

78 "This is a sample file.\n" 

79 "You selected a banana.\n" 

80 ) 

81 

82 assert len(atexit_callables) == 2 

83 for c in atexit_callables: 

84 c()