Coverage for cogapp/test_makefiles.py: 23.38%

1""" Test the cogapp.makefiles modules

2 http://nedbatchelder.com/code/cog

3

4 Copyright 2004-2019, Ned Batchelder.

5"""

6

7 from __future__ import absolute_import

8

9 import shutil

10 import os

11 import random

12 import tempfile

13

14 from . import makefiles

15 from .backward import TestCase

16

17

18 class SimpleTests(TestCase):

19

20 def setUp(self):

21 # Create a temporary directory.

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

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

24 os.mkdir(self.tempdir)

25

26 def tearDown(self):

27 # Get rid of the temporary directory.

28 shutil.rmtree(self.tempdir)

29

30 def exists(self, dname, fname):

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

32

33 def checkFilesExist(self, d, dname):

34 for fname in d.keys():

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

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

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

38

39 def checkFilesDontExist(self, d, dname):

40 for fname in d.keys():

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

42

43 def testOneFile(self):

44 fname = 'foo.txt'

45 notfname = 'not_here.txt'

46 d = { fname: "howdy" }

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

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

49

50 makefiles.makeFiles(d, self.tempdir)

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

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

53

54 makefiles.removeFiles(d, self.tempdir)

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

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

57

58 def testManyFiles(self):

59 d = {

60 'top1.txt': "howdy",

61 'top2.txt': "hello",

62 'sub': {

63 'sub1.txt': "inside",

64 'sub2.txt': "inside2",

65 },

66 }

67

68 self.checkFilesDontExist(d, self.tempdir)

69 makefiles.makeFiles(d, self.tempdir)

70 self.checkFilesExist(d, self.tempdir)

71 makefiles.removeFiles(d, self.tempdir)

72 self.checkFilesDontExist(d, self.tempdir)

73

74 def testOverlapping(self):

75 d1 = {

76 'top1.txt': "howdy",

77 'sub': {

78 'sub1.txt': "inside",

79 },

80 }

81

82 d2 = {

83 'top2.txt': "hello",

84 'sub': {

85 'sub2.txt': "inside2",

86 },

87 }

88

89 self.checkFilesDontExist(d1, self.tempdir)

90 self.checkFilesDontExist(d2, self.tempdir)

91 makefiles.makeFiles(d1, self.tempdir)

92 makefiles.makeFiles(d2, self.tempdir)

93 self.checkFilesExist(d1, self.tempdir)

94 self.checkFilesExist(d2, self.tempdir)

95 makefiles.removeFiles(d1, self.tempdir)

96 makefiles.removeFiles(d2, self.tempdir)

97 self.checkFilesDontExist(d1, self.tempdir)

98 self.checkFilesDontExist(d2, self.tempdir)

99

100 def testContents(self):

101 fname = 'bar.txt'

102 cont0 = "I am bar.txt"

103 d = { fname: cont0 }

104 makefiles.makeFiles(d, self.tempdir)

105 fcont1 = open(os.path.join(self.tempdir, fname))

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

107 fcont1.close()

108

109 def testDedent(self):

110 fname = 'dedent.txt'

111 d = {

112 fname: """\

113 This is dedent.txt

114 \tTabbed in.

115 spaced in.

116 OK.

117 """,

118 }

119 makefiles.makeFiles(d, self.tempdir)

120 fcont = open(os.path.join(self.tempdir, fname))

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

122 fcont.close()