Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

""" Test the cogapp.makefiles modules 

http://nedbatchelder.com/code/cog 

 

Copyright 2004-2019, Ned Batchelder. 

""" 

 

from __future__ import absolute_import 

 

import shutil 

import os 

import random 

import tempfile 

 

from . import makefiles 

from .backward import TestCase 

 

 

class SimpleTests(TestCase): 

 

def setUp(self): 

# Create a temporary directory. 

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

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

os.mkdir(self.tempdir) 

 

def tearDown(self): 

# Get rid of the temporary directory. 

shutil.rmtree(self.tempdir) 

 

def exists(self, dname, fname): 

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

 

def checkFilesExist(self, d, dname): 

for fname in d.keys(): 

assert(self.exists(dname, fname)) 

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

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

 

def checkFilesDontExist(self, d, dname): 

for fname in d.keys(): 

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

 

def testOneFile(self): 

fname = 'foo.txt' 

notfname = 'not_here.txt' 

d = { fname: "howdy" } 

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

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

 

makefiles.makeFiles(d, self.tempdir) 

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

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

 

makefiles.removeFiles(d, self.tempdir) 

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

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

 

def testManyFiles(self): 

d = { 

'top1.txt': "howdy", 

'top2.txt': "hello", 

'sub': { 

'sub1.txt': "inside", 

'sub2.txt': "inside2", 

}, 

} 

 

self.checkFilesDontExist(d, self.tempdir) 

makefiles.makeFiles(d, self.tempdir) 

self.checkFilesExist(d, self.tempdir) 

makefiles.removeFiles(d, self.tempdir) 

self.checkFilesDontExist(d, self.tempdir) 

 

def testOverlapping(self): 

d1 = { 

'top1.txt': "howdy", 

'sub': { 

'sub1.txt': "inside", 

}, 

} 

 

d2 = { 

'top2.txt': "hello", 

'sub': { 

'sub2.txt': "inside2", 

}, 

} 

 

self.checkFilesDontExist(d1, self.tempdir) 

self.checkFilesDontExist(d2, self.tempdir) 

makefiles.makeFiles(d1, self.tempdir) 

makefiles.makeFiles(d2, self.tempdir) 

self.checkFilesExist(d1, self.tempdir) 

self.checkFilesExist(d2, self.tempdir) 

makefiles.removeFiles(d1, self.tempdir) 

makefiles.removeFiles(d2, self.tempdir) 

self.checkFilesDontExist(d1, self.tempdir) 

self.checkFilesDontExist(d2, self.tempdir) 

 

def testContents(self): 

fname = 'bar.txt' 

cont0 = "I am bar.txt" 

d = { fname: cont0 } 

makefiles.makeFiles(d, self.tempdir) 

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

assert(fcont1.read() == cont0) 

fcont1.close() 

 

def testDedent(self): 

fname = 'dedent.txt' 

d = { 

fname: """\ 

This is dedent.txt 

\tTabbed in. 

spaced in. 

OK. 

""", 

} 

makefiles.makeFiles(d, self.tempdir) 

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

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

fcont.close()