예제 #1
0
    def setUp(self):
        """Common initialization for tests.

        Creates a fake home directory to work with and have a dot file
        manager object set up with it.
        """
        self.tmp_dir = tempfile.mkdtemp()
        home_dir = os.path.join(self.tmp_dir, 'home')
        os.mkdir(home_dir)
        dotfiles_dir = os.path.join(home_dir, 'dotfiles')
        os.mkdir(dotfiles_dir)
        self.dfm = DotfileManager(home_dir=home_dir,
                                  dotfiles_dir=os.path.basename(dotfiles_dir))
        logging.disable(logging.WARNING)
예제 #2
0
class DotfileManagerTest(unittest.TestCase):  # pylint: disable=R0904

    """Test case for DotfileManager."""

    def setUp(self):
        """Common initialization for tests.

        Creates a fake home directory to work with and have a dot file
        manager object set up with it.
        """
        self.tmp_dir = tempfile.mkdtemp()
        home_dir = os.path.join(self.tmp_dir, 'home')
        os.mkdir(home_dir)
        dotfiles_dir = os.path.join(home_dir, 'dotfiles')
        os.mkdir(dotfiles_dir)
        self.dfm = DotfileManager(home_dir=home_dir,
                                  dotfiles_dir=os.path.basename(dotfiles_dir))
        logging.disable(logging.WARNING)

    def tearDown(self):
        """Cleans up test home and dotfiles directories."""
        shutil.rmtree(self.tmp_dir)

    def assert_exists(self, path):
        """Helper to assert a path exists."""
        self.assertTrue(os.path.exists(path), "{} does not exist".format(path))

    def assert_dir_exists(self, directory):
        """Helper to assert a path exists and is a directory."""
        self.assert_exists(directory)
        self.assertTrue(os.path.isdir(directory),
                        "{} is not a directory".format(directory))

    def create_dir(self, dirname, newdirname):
        """Creates a directory."""
        if not os.path.exists(dirname):
            dirname = os.path.join(self.dfm.home_dir, dirname)
        dirpath = os.path.join(dirname, newdirname)
        os.mkdir(dirpath)

    def create_symlink(self, dirname, filename, target):
        """Creates a symlink."""
        filepath = os.path.join(dirname, filename)
        os.symlink(target, filepath)

    def test_home_dir_exists(self):
        """Checks the home directory is set up and is a directory."""
        self.assert_dir_exists(self.dfm.home_dir)

    def test_dotfile_dir_exists(self):
        """Checks the dotfile directory is set up and is a directory."""
        self.assert_dir_exists(self.dfm.get_dotfiles_abspath())

    def test_status_dir_synced(self):
        """Checks status of synced (dot)subdirectories."""
        subdir_name = 'subdir'
        self.create_dir(self.dfm.dotfiles_dir, subdir_name)
        target = os.path.join(self.dfm.dotfiles_dir, subdir_name)
        self.create_symlink(self.dfm.home_dir, subdir_name, target)
        dotfiles = list(self.dfm.get_dotfiles())
        expected_dotfile = Dotfile(subdir_name, status=Dotfile.synced)
        self.assertEqual(dotfiles, [expected_dotfile])