github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/tools/noms/staging_test.py (about) 1 #!/usr/bin/python 2 3 # Copyright 2016 Attic Labs, Inc. All rights reserved. 4 # Licensed under the Apache License, version 2.0: 5 # http://www.apache.org/licenses/LICENSE-2.0 6 7 import os, os.path, shutil, tempfile, unittest 8 import staging 9 10 class TestStaging(unittest.TestCase): 11 def setUp(self): 12 self.tempdir = os.path.realpath(tempfile.mkdtemp()) 13 self.nested = tempfile.mkdtemp(dir=self.tempdir) 14 15 def tearDown(self): 16 shutil.rmtree(self.tempdir, ignore_errors=True) 17 18 def test_Nested(self): 19 self.assertTrue(staging._is_sub_dir(self.nested, self.tempdir)) 20 21 def test_NotNested(self): 22 otherNested = tempfile.mkdtemp(dir=self.tempdir) 23 self.assertFalse(staging._is_sub_dir(self.nested, otherNested)) 24 25 def test_DotDotNotReallyNested(self): 26 notReallyNested = os.path.join(self.tempdir, 'foo', os.pardir, 'bar') 27 self.assertFalse(staging._is_sub_dir(self.nested, notReallyNested)) 28 29 def test_LinkNotReallyNested(self): 30 otherNested = tempfile.mkdtemp(dir=self.tempdir) 31 linkName = os.path.join(self.nested, 'link') 32 os.symlink(otherNested, linkName) 33 self.assertFalse(staging._is_sub_dir(linkName, self.nested)) 34 35 def test_DirPath(self): 36 linkName = os.path.join(self.tempdir, 'link') 37 os.symlink(self.nested, linkName) 38 norm = staging._dir_path(linkName) 39 self.assertEqual(self.nested, norm) 40 41 def test_DirPathFails(self): 42 f = tempfile.NamedTemporaryFile(dir=self.tempdir) 43 try: 44 staging._dir_path(f.name) 45 except ValueError: 46 pass 47 48 def test_GlobCopier(self): 49 files = ( 50 'a.js', 51 'b.js', 52 'c.html', 53 'd.css', 54 'webpack.config.js', 55 56 'x/aa.js', 57 'x/bb.js', 58 'x/dd.css', 59 'x/webpack.config.js', 60 61 'x/xx/aaa.js', 62 'x/xx/bbb.js', 63 'x/xx/webpack.config.js', 64 65 'x/yy/aaa.js', 66 'x/yy/bbb.js', 67 'x/yy/webpack.config.js', 68 69 'y/aaaa.js', 70 'y/bbbb.js', 71 'y/webpack.config.js', 72 73 'y/xxx/a5.js', 74 'y/xxx/b5.js', 75 'y/xxx/webpack.config.js', 76 77 'z/a6.js', 78 'z/b6.js', 79 'z/webpack.config.js', 80 ) 81 for d in ('x/xx', 'x/yy', 'y/xxx', 'z'): 82 os.makedirs(os.path.join(self.tempdir, d)) 83 for name in files: 84 with open(os.path.join(self.tempdir, name), 'w') as f: 85 f.write('hi') 86 87 cwd = os.getcwd() 88 try: 89 os.chdir(self.tempdir) 90 staging.GlobCopier('*.js', 'c.html', 'x/*.js', 'x/xx/*', 'y/*', 'y/*')(self.nested) 91 finally: 92 os.chdir(cwd) 93 94 self.assertEqual(sorted(['a.js', 'b.js', 'c.html', 'x', 'y']), 95 sorted(os.listdir(self.nested))) 96 self.assertEqual(sorted(['aa.js', 'bb.js', 'xx']), 97 sorted(os.listdir(os.path.join(self.nested, 'x')))) 98 self.assertEqual(sorted(['aaa.js', 'bbb.js']), 99 sorted(os.listdir(os.path.join(self.nested, 'x/xx')))) 100 self.assertEqual(sorted(['aaaa.js', 'bbbb.js']), 101 sorted(os.listdir(os.path.join(self.nested, 'y')))) 102 103 def test_GlobCopierWithRename(self): 104 files = ( 105 'a.js', 106 'b.js', 107 'c.html', 108 'd.css', 109 'webpack.config.js', 110 111 'x/aa.js', 112 'x/bb.js', 113 'x/dd.css', 114 'x/webpack.config.js', 115 116 'x/xx/aaa.js', 117 'x/xx/bbb.js', 118 'x/xx/webpack.config.js', 119 120 'x/yy/aaa.js', 121 'x/yy/bbb.js', 122 'x/yy/webpack.config.js', 123 124 'y/aaaa.js', 125 'y/bbbb.js', 126 'y/webpack.config.js', 127 128 'y/xxx/a5.js', 129 'y/xxx/b5.js', 130 'y/xxx/webpack.config.js', 131 132 'z/a6.js', 133 'z/b6.js', 134 'z/webpack.config.js', 135 ) 136 with open(os.path.join(self.tempdir, 'index.html'), 'w') as f: 137 f.write('index.html') 138 for d in ('x/xx', 'x/yy', 'y/xxx', 'z'): 139 os.makedirs(os.path.join(self.tempdir, d)) 140 for name in files: 141 with open(os.path.join(self.tempdir, name), 'w') as f: 142 f.write('hi! name: ' + name) 143 144 cwd = os.getcwd() 145 try: 146 os.chdir(self.tempdir) 147 staging.GlobCopier( 148 '*.js', 'c.html', 'x/*.js', 'x/xx/*', 'y/*', 'y/*', 149 index_file='index.html', 150 rename=True)(self.nested) 151 finally: 152 os.chdir(cwd) 153 154 self.assertEqual(sorted(['a.702f720d2b49bd41c30f.js', 'b.49cf685c13e7de516ebc.js', 155 'c.fe1a3b03473494234e2d.html', 'index.html', 'x', 'y']), 156 sorted(os.listdir(self.nested))) 157 self.assertEqual(sorted(['aa.eb0f5ae6432d325f9448.js', 158 'bb.480969faecf03a9eb729.js', 'xx']), 159 sorted(os.listdir(os.path.join(self.nested, 'x')))) 160 self.assertEqual(sorted(['aaa.a9810946370699474422.js', 161 'bbb.c06f75d2d61cb6717b2c.js']), 162 sorted(os.listdir(os.path.join(self.nested, 'x/xx')))) 163 self.assertEqual(sorted(['aaaa.a68d3caf6e0e971ab96f.js', 164 'bbbb.84bd5947630aca231726.js']), 165 sorted(os.listdir(os.path.join(self.nested, 'y')))) 166 167 if __name__ == '__main__': 168 unittest.main()