def test_get_config_directory(self):
        old_isfile = os.path.isfile
        old_isdir = os.path.isdir

        section = Section("default")

        # Without section
        config_dir = get_config_directory(None)
        self.assertEqual(config_dir, os.getcwd())

        # With section, but without "config"
        os.path.isfile = lambda *args: True
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, os.getcwd())

        os.path.isfile = lambda *args: False
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, None)

        # With "config" in section
        section.append(Setting("config", "/path/to/dir/config"))

        os.path.isdir = lambda *args: True
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, "/path/to/dir/config")

        os.path.isdir = lambda *args: False
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, "/path/to/dir")

        os.path.isdir = old_isdir
        os.path.isfile = old_isfile
Example #2
0
    def test_get_config_directory(self):
        old_isfile = os.path.isfile
        old_isdir = os.path.isdir

        section = Section("default")

        # Without section
        config_dir = get_config_directory(None)
        self.assertEqual(config_dir, os.getcwd())

        # With section, but without "config"
        os.path.isfile = lambda *args: True
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, os.getcwd())

        os.path.isfile = lambda *args: False
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, None)

        # With "config" in section
        section.append(Setting("config", "/path/to/dir/config"))

        os.path.isdir = lambda *args: True
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, "/path/to/dir/config")

        os.path.isdir = lambda *args: False
        config_dir = get_config_directory(section)
        self.assertEqual(config_dir, "/path/to/dir")

        os.path.isdir = old_isdir
        os.path.isfile = old_isfile
Example #3
0
def main(log_printer=None, section: Section = None):
    start_path = get_config_directory(section)
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    if start_path is None:
        log_printer.err("Can only delete .orig files if .coafile is found")
        return 255

    orig_files = Globbing.glob(
        os.path.abspath(os.path.join(start_path, '**', '*.orig')))

    not_deleted = 0
    for ofile in orig_files:
        log_printer.info("Deleting old backup file... " +
                         os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except OSError as oserror:
            not_deleted += 1
            log_printer.warn("Couldn't delete... {}. {}".format(
                os.path.relpath(ofile), oserror.strerror))

    if not_deleted:
        log_printer.warn(
            str(not_deleted) + " .orig backup files could not be"
            " deleted, possibly because you lack the permission"
            " to do so. coala may not be able to create"
            " backup files when patches are applied.")
    return 0
Example #4
0
def main(log_printer=None, section: Section = None):
    configure_logging()

    start_path = get_config_directory(section)

    if start_path is None:
        return 255

    # start_path may have unintended glob characters
    orig_files = Globbing.glob(os.path.join(
        glob_escape(start_path), '**', '*.orig'))

    not_deleted = 0
    for ofile in orig_files:
        logging.info('Deleting old backup file... ' + os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except OSError as oserror:
            not_deleted += 1
            logging.warning("Couldn't delete {}. {}".format(
                os.path.relpath(ofile), oserror.strerror))

    if not_deleted:
        logging.warning(str(not_deleted) + ' .orig backup files could not be'
                                           ' deleted, possibly because you '
                                           'lack the permission to do so. '
                                           'coala may not be able to create'
                                           ' backup files when patches are '
                                           'applied.')
    return 0
def main(log_printer=None, section: Section=None):
    start_path = get_config_directory(section)
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    if start_path is None:
        return 255

    # start_path may have unintended glob characters
    orig_files = Globbing.glob(os.path.join(
        glob_escape(start_path), '**', '*.orig'))

    not_deleted = 0
    for ofile in orig_files:
        log_printer.info("Deleting old backup file... "
                         + os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except OSError as oserror:
            not_deleted += 1
            log_printer.warn("Couldn't delete {}. {}".format(
                os.path.relpath(ofile), oserror.strerror))

    if not_deleted:
        log_printer.warn(str(not_deleted) + " .orig backup files could not be"
                         " deleted, possibly because you lack the permission"
                         " to do so. coala may not be able to create"
                         " backup files when patches are applied.")
    return 0
Example #6
0
def main(log_printer=None, section: Section = None):
    configure_logging()

    start_path = get_config_directory(section)
    log_printer = (LogPrinter(ConsolePrinter())
                   if log_printer is None else log_printer)

    if start_path is None:
        return 255

    # start_path may have unintended glob characters
    orig_files = Globbing.glob(
        os.path.join(glob_escape(start_path), '**', '*.orig'))

    not_deleted = 0
    for ofile in orig_files:
        log_printer.info('Deleting old backup file... ' +
                         os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except OSError as oserror:
            not_deleted += 1
            log_printer.warn("Couldn't delete {}. {}".format(
                os.path.relpath(ofile), oserror.strerror))

    if not_deleted:
        log_printer.warn(
            str(not_deleted) + ' .orig backup files could not be'
            ' deleted, possibly because you lack the permission'
            ' to do so. coala may not be able to create'
            ' backup files when patches are applied.')
    return 0
Example #7
0
def main(log_printer=None, section: Section=None):
    start_path = get_config_directory(section)
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    if start_path is None:
        log_printer.err("Can only delete .orig files if .coafile is found")
        return 255

    orig_files = Globbing.glob(os.path.abspath(
        os.path.join(start_path, '**', '*.orig')))

    not_deleted = 0
    for ofile in orig_files:
        log_printer.info("Deleting old backup file... "
                         + os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except:
            not_deleted += 1
            log_printer.warn("Couldn't delete... " + os.path.relpath(ofile))

    if not_deleted:
        log_printer.warn(str(not_deleted) + " .orig backup files could not be"
                         " deleted, possibly because you lack the permission"
                         " to do so. coala may not be able to create"
                         " backup files when patches are applied.")
    return 0
Example #8
0
    def get_config_dir(self):
        """
        Gives the directory where the configuration file is.

        :return: Directory of the config file.
        """
        return get_config_directory(self.section)
Example #9
0
    def get_config_dir(self):
        """
        Gives the directory where the configuration file is.

        :return: Directory of the config file.
        """
        return get_config_directory(self.section)
Example #10
0
 def test_bear_dirs(self):
     section = Section("section", None)
     empty_bear_dirs_len = len(section.bear_dirs())
     section.append(Setting("bear_dirs", "test1, test2 (1)"))
     self.assertEqual(len(section.bear_dirs()), empty_bear_dirs_len + 2)
     # Verify if bear directories are properly escaped
     root = get_config_directory(section)
     path = os.path.join(glob_escape(root), glob_escape("test2 (1)"), "**")
     self.assertIn(path, section.bear_dirs())
Example #11
0
 def test_bear_dirs(self):
     section = Section('section', None)
     empty_bear_dirs_len = len(section.bear_dirs())
     section.append(Setting('bear_dirs', 'test1, test2 (1)'))
     self.assertEqual(len(section.bear_dirs()), empty_bear_dirs_len + 2)
     # Verify if bear directories are properly escaped
     root = get_config_directory(section)
     path = os.path.join(glob_escape(root), glob_escape('test2 (1)'), '**')
     self.assertIn(path, section.bear_dirs())
Example #12
0
 def test_bear_dirs(self):
     section = Section('section', None)
     empty_bear_dirs_len = len(section.bear_dirs())
     section.append(Setting('bear_dirs', 'test1, test2 (1)'))
     self.assertEqual(len(section.bear_dirs()), empty_bear_dirs_len + 2)
     # Verify if bear directories are properly escaped
     root = get_config_directory(section)
     path = os.path.join(glob_escape(root), glob_escape('test2 (1)'), '**')
     self.assertIn(path, section.bear_dirs())
Example #13
0
 def test_bear_dirs(self):
     section = Section("section", None)
     empty_bear_dirs_len = len(section.bear_dirs())
     section.append(Setting("bear_dirs", "test1, test2 (1)"))
     self.assertEqual(len(section.bear_dirs()), empty_bear_dirs_len + 2)
     # Verify if bear directories are properly escaped
     root = get_config_directory(section)
     path = os.path.join(glob_escape(root), glob_escape("test2 (1)"), "**")
     self.assertIn(path, section.bear_dirs())
Example #14
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit("A shortlog that is too long is not good for history")
     os.chdir(no_git_dir)
     # When section doesn't have a config setting
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], "git:")
     # when section does have a config setting
     self.section.append(Setting("config", re.escape(self.gitdir)))
     self.assertEqual(self.run_uut(),
                      ["Shortlog of HEAD commit is too long."])
     self.assertEqual(get_config_directory(self.section), self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
Example #15
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit("A shortlog that is too long is not good for history")
     os.chdir(no_git_dir)
     # When section doesn't have a config setting
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], "git:")
     # when section does have a config setting
     self.section.append(Setting("config", re.escape(self.gitdir)))
     self.assertEqual(self.run_uut(),
                      ["Shortlog of HEAD commit is too long."])
     self.assertEqual(get_config_directory(self.section),
                      self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
Example #16
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit("Add a very long shortlog for a bad project history.")
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], "git:")
     # when section does have a project_dir
     self.section.append(Setting("project_dir", escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(),
                      ["Shortlog of HEAD commit is 1 character(s) longer "
                       "than the limit (51 > 50)."])
     self.assertEqual(get_config_directory(self.section),
                      self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
Example #17
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit("Add a very long shortlog for a bad project history.")
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], "git:")
     # when section does have a project_dir
     self.section.append(Setting("project_dir", escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(),
                      ["Shortlog of HEAD commit is 1 character(s) longer "
                       "than the limit (51 > 50)."])
     self.assertEqual(get_config_directory(self.section),
                      self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit('Add a very long shortlog for a bad project history.')
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], 'git:')
     # when section does have a project_dir
     self.section.append(Setting('project_dir', escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(),
                      ['Shortlog of the HEAD commit contains 51 '
                       'character(s). This is 1 character(s) longer than '
                       'the limit (51 > 50).'])
     self.assertEqual(get_config_directory(self.section),
                      self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
Example #19
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit('Add a very long shortlog for a bad project history.')
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], 'git:')
     # when section does have a project_dir
     self.section.append(Setting('project_dir', escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(), [
         'Shortlog of the HEAD commit contains 51 '
         'character(s). This is 1 character(s) longer than '
         'the limit (51 > 50).'
     ])
     self.assertEqual(get_config_directory(self.section), self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)