github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/tools/noms/symlink_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 symlink 9 10 class TestForceSymlink(unittest.TestCase): 11 CONTENTS = 'test file contents' 12 13 def setUp(self): 14 self.tempdir = tempfile.mkdtemp() 15 self.source = tempfile.NamedTemporaryFile(dir=self.tempdir, delete=False) 16 with self.source.file as f: 17 f.write(self.CONTENTS) 18 19 20 def tearDown(self): 21 shutil.rmtree(self.tempdir, ignore_errors=True) 22 23 24 def verifySymlink(self, linkName): 25 with open(linkName, 'r') as f: 26 actual = f.read() 27 self.assertEqual(self.CONTENTS, actual) 28 29 30 def test_ClobberFile(self): 31 linkName = os.path.join(self.tempdir, 'link') 32 with open(linkName, 'w') as f: 33 f.write('foo') 34 35 symlink.Force(self.source.name, linkName) 36 self.verifySymlink(linkName) 37 38 39 def test_ClobberSymlink(self): 40 linkName = os.path.join(self.tempdir, 'link') 41 os.symlink('nowhere', linkName) 42 43 symlink.Force(self.source.name, linkName) 44 self.verifySymlink(linkName) 45 46 47 def test_NoClobberDir(self): 48 linkName = os.path.join(self.tempdir, 'link') 49 os.mkdir(linkName, 0777) 50 51 try: 52 symlink.Force(self.source.name, linkName) 53 except symlink.LinkError: 54 pass 55 56 57 if __name__ == '__main__': 58 unittest.main()