Coverage for cogapp/test_makefiles.py: 22.97%

68 statements  

« prev     ^ index     » next       coverage.py v7.5.0a1.dev1, created at 2024-04-15 15:50 -0400

1""" Test the cogapp.makefiles modules 

2""" 

3 

4import shutil 

5import os 

6import random 

7import tempfile 

8from unittest import TestCase 

9 

10from . import makefiles 

11 

12 

13class SimpleTests(TestCase): 

14 

15 def setUp(self): 

16 # Create a temporary directory. 

17 my_dir = 'testmakefiles_tempdir_' + str(random.random())[2:] 

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

19 os.mkdir(self.tempdir) 

20 

21 def tearDown(self): 

22 # Get rid of the temporary directory. 

23 shutil.rmtree(self.tempdir) 

24 

25 def exists(self, dname, fname): 

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

27 

28 def checkFilesExist(self, d, dname): 

29 for fname in d.keys(): 

30 assert(self.exists(dname, fname)) 

31 if type(d[fname]) == type({}): 

32 self.checkFilesExist(d[fname], os.path.join(dname, fname)) 

33 

34 def checkFilesDontExist(self, d, dname): 

35 for fname in d.keys(): 

36 assert(not self.exists(dname, fname)) 

37 

38 def testOneFile(self): 

39 fname = 'foo.txt' 

40 notfname = 'not_here.txt' 

41 d = { fname: "howdy" } 

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

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

44 

45 makefiles.makeFiles(d, self.tempdir) 

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

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

48 

49 makefiles.removeFiles(d, self.tempdir) 

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

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

52 

53 def testManyFiles(self): 

54 d = { 

55 'top1.txt': "howdy", 

56 'top2.txt': "hello", 

57 'sub': { 

58 'sub1.txt': "inside", 

59 'sub2.txt': "inside2", 

60 }, 

61 } 

62 

63 self.checkFilesDontExist(d, self.tempdir) 

64 makefiles.makeFiles(d, self.tempdir) 

65 self.checkFilesExist(d, self.tempdir) 

66 makefiles.removeFiles(d, self.tempdir) 

67 self.checkFilesDontExist(d, self.tempdir) 

68 

69 def testOverlapping(self): 

70 d1 = { 

71 'top1.txt': "howdy", 

72 'sub': { 

73 'sub1.txt': "inside", 

74 }, 

75 } 

76 

77 d2 = { 

78 'top2.txt': "hello", 

79 'sub': { 

80 'sub2.txt': "inside2", 

81 }, 

82 } 

83 

84 self.checkFilesDontExist(d1, self.tempdir) 

85 self.checkFilesDontExist(d2, self.tempdir) 

86 makefiles.makeFiles(d1, self.tempdir) 

87 makefiles.makeFiles(d2, self.tempdir) 

88 self.checkFilesExist(d1, self.tempdir) 

89 self.checkFilesExist(d2, self.tempdir) 

90 makefiles.removeFiles(d1, self.tempdir) 

91 makefiles.removeFiles(d2, self.tempdir) 

92 self.checkFilesDontExist(d1, self.tempdir) 

93 self.checkFilesDontExist(d2, self.tempdir) 

94 

95 def testContents(self): 

96 fname = 'bar.txt' 

97 cont0 = "I am bar.txt" 

98 d = { fname: cont0 } 

99 makefiles.makeFiles(d, self.tempdir) 

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

101 assert(fcont1.read() == cont0) 

102 

103 def testDedent(self): 

104 fname = 'dedent.txt' 

105 d = { 

106 fname: """\ 

107 This is dedent.txt 

108 \tTabbed in. 

109 spaced in. 

110 OK. 

111 """, 

112 } 

113 makefiles.makeFiles(d, self.tempdir) 

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

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