def test_invalid_multiple_basenames(self):
        '''
        Tests the behavior where multiple components at the end of the path are invalid.
        '''
        dirname = os.path.dirname(__file__)
        dirname2 = path_utils.versioned_name(dirname, 'foo')
        test_path = path_utils.versioned_name(dirname2, 'goo')

        output = path_utils.valid_split(test_path)
        expected_output = (dirname, os.path.basename(dirname2))

        self.assertEqual(output, expected_output)
    def test_invalid_path(self):
        '''
        Tests behavior with a 100% invalid path.
        '''
        test_path = path_utils.versioned_name('', 'foo')

        output = path_utils.valid_split(test_path)
        expected_output = ('', test_path)

        self.assertEqual(output, expected_output)
 def test_at_end(self):
     '''
     Test that versions added at the very end of the name (regardless of the existence of an
     extension) if the at_end option is specified.
     '''
     with open(os.path.join(self.directory, 'foo.txt'), 'w'):
         pass
     output = path_utils.versioned_name(self.directory, 'foo.txt', at_end=True)
     expected_output = os.path.join(self.directory, 'foo.txt_0')
     self.assertEqual(output, expected_output)
    def test_invalid_basename(self):
        '''
        Tests the behavior where the basename is invalid.
        '''
        dirname = os.path.dirname(__file__)
        test_path = path_utils.versioned_name(dirname, 'foo')

        output = path_utils.valid_split(test_path)
        expected_output = (dirname, os.path.basename(test_path))

        self.assertEqual(output, expected_output)
    def test_versioning(self):
        '''
        Tests that versions are added and incremented to make unique names.
        '''
        # Test that no version is added if the path doesn't exist.
        output = path_utils.versioned_name(self.directory, 'foo')
        expected_output = os.path.join(self.directory, 'foo')
        self.assertEqual(output, expected_output)

        # Test that a file causes the version to increment.
        with open(expected_output, 'w'):
            pass
        output = path_utils.versioned_name(self.directory, 'foo')
        expected_output = os.path.join(self.directory, 'foo_0')
        self.assertEqual(output, expected_output)

        # Test that a directory causes the version to increment.
        os.mkdir(os.path.join(self.directory, 'foo_0'))
        output = path_utils.versioned_name(self.directory, 'foo')
        expected_output = os.path.join(self.directory, 'foo_1')
        self.assertEqual(output, expected_output)
    def test_extensions(self):
        '''
        Test that the version number is added before file extensions.
        '''
        # Test version added before file extensions.
        with open(os.path.join(self.directory, 'foo.txt'), 'w'):
            pass
        output = path_utils.versioned_name(self.directory, 'foo.txt')
        expected_output = os.path.join(self.directory, 'foo_0.txt')
        self.assertEqual(output, expected_output)

        # Test version added before extension of files starting with "."
        with open(os.path.join(self.directory, '.foo.txt'), 'w'):
            pass
        output = path_utils.versioned_name(self.directory, '.foo.txt')
        expected_output = os.path.join(self.directory, '.foo_0.txt')
        self.assertEqual(output, expected_output)

        # Test version added at end of name of files starting with . if there is no extension.
        with open(os.path.join(self.directory, '.foo'), 'w'):
            pass
        output = path_utils.versioned_name(self.directory, '.foo')
        expected_output = os.path.join(self.directory, '.foo_0')
        self.assertEqual(output, expected_output)
    def test_completions(self):
        '''
        Test path completions.
        '''
        # Test 100% invalid path
        test_path = path_utils.versioned_name('', 'foo')
        output = path_utils.complete_path(test_path)
        expected_output = []
        self.assertEqual(output, expected_output)

        # Test an invalid path ending in a separator
        test_path = path_utils.versioned_name('', 'foo/')
        output = path_utils.complete_path(test_path)
        expected_output = []
        self.assertEqual(output, expected_output)

        test_directory = tempfile.mkdtemp()

        # Test valid unambiguous path
        test_path = os.path.join(test_directory, 'unambiguous_dir')
        os.mkdir(test_path)
        output = path_utils.complete_path(test_path)
        expected_output = [test_path]
        self.assertEqual(output, expected_output)

        # Test valid unambiguous path ending in a separator
        test_path = os.path.join(test_directory, 'unambiguous_dir/')
        output = path_utils.complete_path(test_path)
        expected_output = [test_path]
        self.assertEqual(output, expected_output)

        # Test valid ambiguous path
        ambiguous_test_path_1 = os.path.join(test_directory, 'ambiguous_dir')
        os.mkdir(ambiguous_test_path_1)
        ambiguous_test_path_2 = os.path.join(test_directory, 'ambiguous_dir1')
        os.mkdir(ambiguous_test_path_2)
        output = path_utils.complete_path(ambiguous_test_path_1)
        expected_output = [ambiguous_test_path_1, ambiguous_test_path_2]
        self.assertEqual(set(output), set(expected_output))

        # Test valid ambiguous path ending in a separator
        test_path_1 = os.path.join(test_directory, 'ambiguous_dir/')
        output = path_utils.complete_path(test_path_1)
        expected_output = [test_path_1]
        self.assertEqual(output, expected_output)

        # Test partially invalid path with no possible completions.
        test_path = os.path.join(test_directory, 'foo/goo/moo')
        output = path_utils.complete_path(test_path)
        expected_output = []
        self.assertEqual(output, expected_output)

        # Test partially invalid path with possible completions.
        test_path = os.path.join(test_directory, 'ambig')
        output = path_utils.complete_path(test_path)
        expected_output = [ambiguous_test_path_1, ambiguous_test_path_2]
        self.assertEqual(set(output), set(expected_output))

        # Test partially invalid path ending with a separator.
        test_path = os.path.join(test_directory, 'ambig/')
        output = path_utils.complete_path(test_path)
        expected_output = [ambiguous_test_path_1, ambiguous_test_path_2]
        self.assertEqual(set(output), set(expected_output))

        shutil.rmtree(test_directory)