1# fixtures: Fixtures with cleanups for testing and convenience. 2# 3# Copyright (c) 2011 Canonical Ltd. 4# 5# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause 6# license at the users choice. A copy of both licenses are available in the 7# project source as Apache-2.0 and BSD. You may not use this file except in 8# compliance with one of these two licences. 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# license you chose for the specific language governing permissions and 14# limitations under that license. 15 16import os 17 18import testtools 19from testtools.matchers import StartsWith 20 21from fixtures import ( 22 TempDir, 23 TempHomeDir, 24 ) 25 26class TestTempDir(testtools.TestCase): 27 28 def test_basic(self): 29 fixture = TempHomeDir() 30 sentinel = object() 31 self.assertEqual(sentinel, getattr(fixture, 'path', sentinel)) 32 fixture.setUp() 33 try: 34 path = fixture.path 35 self.assertTrue(os.path.isdir(path)) 36 self.assertEqual(path, os.environ.get("HOME")) 37 finally: 38 fixture.cleanUp() 39 self.assertFalse(os.path.isdir(path)) 40 41 def test_under_dir(self): 42 root = self.useFixture(TempDir()).path 43 fixture = TempHomeDir(root) 44 fixture.setUp() 45 with fixture: 46 self.assertThat(fixture.path, StartsWith(root)) 47