Coverage for cogapp/test_makefiles.py: 22.97%

68 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-06 06:21 -0500

1"""Test the cogapp.makefiles modules""" 

2 

3import shutil 

4import os 

5import random 

6import tempfile 

7from unittest import TestCase 

8 

9from . import makefiles 

10 

11 

12class SimpleTests(TestCase): 

13 def setUp(self): 

14 # Create a temporary directory. 

15 my_dir = "testmakefiles_tempdir_" + str(random.random())[2:] 

16 self.tempdir = os.path.join(tempfile.gettempdir(), my_dir) 

17 os.mkdir(self.tempdir) 

18 

19 def tearDown(self): 

20 # Get rid of the temporary directory. 

21 shutil.rmtree(self.tempdir) 

22 

23 def exists(self, dname, fname): 

24 return os.path.exists(os.path.join(dname, fname)) 

25 

26 def check_files_exist(self, d, dname): 

27 for fname in d.keys(): 

28 assert self.exists(dname, fname) 

29 if isinstance(d[fname], dict): 

30 self.check_files_exist(d[fname], os.path.join(dname, fname)) 

31 

32 def check_files_dont_exist(self, d, dname): 

33 for fname in d.keys(): 

34 assert not self.exists(dname, fname) 

35 

36 def test_one_file(self): 

37 fname = "foo.txt" 

38 notfname = "not_here.txt" 

39 d = {fname: "howdy"} 

40 assert not self.exists(self.tempdir, fname) 

41 assert not self.exists(self.tempdir, notfname) 

42 

43 makefiles.make_files(d, self.tempdir) 

44 assert self.exists(self.tempdir, fname) 

45 assert not self.exists(self.tempdir, notfname) 

46 

47 makefiles.remove_files(d, self.tempdir) 

48 assert not self.exists(self.tempdir, fname) 

49 assert not self.exists(self.tempdir, notfname) 

50 

51 def test_many_files(self): 

52 d = { 

53 "top1.txt": "howdy", 

54 "top2.txt": "hello", 

55 "sub": { 

56 "sub1.txt": "inside", 

57 "sub2.txt": "inside2", 

58 }, 

59 } 

60 

61 self.check_files_dont_exist(d, self.tempdir) 

62 makefiles.make_files(d, self.tempdir) 

63 self.check_files_exist(d, self.tempdir) 

64 makefiles.remove_files(d, self.tempdir) 

65 self.check_files_dont_exist(d, self.tempdir) 

66 

67 def test_overlapping(self): 

68 d1 = { 

69 "top1.txt": "howdy", 

70 "sub": { 

71 "sub1.txt": "inside", 

72 }, 

73 } 

74 

75 d2 = { 

76 "top2.txt": "hello", 

77 "sub": { 

78 "sub2.txt": "inside2", 

79 }, 

80 } 

81 

82 self.check_files_dont_exist(d1, self.tempdir) 

83 self.check_files_dont_exist(d2, self.tempdir) 

84 makefiles.make_files(d1, self.tempdir) 

85 makefiles.make_files(d2, self.tempdir) 

86 self.check_files_exist(d1, self.tempdir) 

87 self.check_files_exist(d2, self.tempdir) 

88 makefiles.remove_files(d1, self.tempdir) 

89 makefiles.remove_files(d2, self.tempdir) 

90 self.check_files_dont_exist(d1, self.tempdir) 

91 self.check_files_dont_exist(d2, self.tempdir) 

92 

93 def test_contents(self): 

94 fname = "bar.txt" 

95 cont0 = "I am bar.txt" 

96 d = {fname: cont0} 

97 makefiles.make_files(d, self.tempdir) 

98 with open(os.path.join(self.tempdir, fname)) as fcont1: 

99 assert fcont1.read() == cont0 

100 

101 def test_dedent(self): 

102 fname = "dedent.txt" 

103 d = { 

104 fname: """\ 

105 This is dedent.txt 

106 \tTabbed in. 

107 spaced in. 

108 OK. 

109 """, 

110 } 

111 makefiles.make_files(d, self.tempdir) 

112 with open(os.path.join(self.tempdir, fname)) as fcont: 

113 assert ( 

114 fcont.read() == "This is dedent.txt\n\tTabbed in.\n spaced in.\nOK.\n" 

115 )