Example #1
0
    def test_run_dir_subconfig_valid(self, mock_print):
        config = deepcopy(CONFIG)
        config["after_imports_new_lines"] = 1

        config_file = self.test_data / IMPORTANIZE_JSON_CONFIG
        config_file.write_text(force_text(json.dumps(config)))

        try:
            consume(
                run(
                    self.test_data,
                    Config(CONFIG),
                    mock.Mock(
                        formatter="grouped",
                        ci=False,
                        print=True,
                        header=False,
                        subconfig=True,
                        length=None,
                        list=False,
                    ),
                )
            )

            mock_print.assert_has_calls(
                [mock.call(self.output_grouped_single_line)]
            )

        finally:
            config_file.unlink()
Example #2
0
    def test_run_dir_subconfig_valid(self, mock_print):
        config = deepcopy(CONFIG)
        config['after_imports_new_lines'] = 1

        config_file = self.test_data / IMPORTANIZE_CONFIG
        config_file.write_text(force_text(json.dumps(config)))

        try:
            actual = run(
                self.test_data,
                Config(CONFIG),
                mock.Mock(formatter='grouped',
                          ci=False,
                          print=True,
                          header=False,
                          subconfig=True),
            )

            self.assertIsNone(actual)
            mock_print.assert_has_calls([
                mock.call(self.output_grouped_single_line),
            ])

        finally:
            config_file.unlink()
Example #3
0
    def test_run_dir_subconfig_invalid(self, mock_print):
        config_file = self.test_data / IMPORTANIZE_JSON_CONFIG
        config_file.write_text("invalid json")

        try:
            consume(
                run(
                    self.test_data,
                    Config(CONFIG),
                    mock.Mock(
                        formatter="grouped",
                        ci=False,
                        print=True,
                        header=False,
                        subconfig=True,
                        length=None,
                        list=False,
                    ),
                )
            )

            mock_print.assert_has_calls([mock.call(self.output_grouped)])

        finally:
            config_file.unlink()
Example #4
0
    def test_find_config_current_dir(self):
        # Instead of the absolute path, assume, user is running importanize
        # from the current directory.

        expected_config = Path(__file__).parent.parent.joinpath(
            IMPORTANIZE_CONFIG)
        # If path is a file, and we have a config for the project and no
        # subconfig, and we dont find the config for the file, return the
        # passed config.
        config = Config.find(Path('tests/test_main.py'))
        self.assertEqual(config.path, expected_config)
Example #5
0
 def test_run_dir_ci(self, mock_print):
     with self.assertRaises(CIFailure):
         run(
             self.test_data,
             Config(CONFIG),
             mock.Mock(formatter='grouped',
                       ci=True,
                       print=True,
                       header=False,
                       subconfig=False),
         )
Example #6
0
    def test_run_file(self, mock_print):
        actual = run(
            self.test_data / 'input.py',
            Config(CONFIG),
            mock.Mock(formatter='grouped',
                      ci=False,
                      print=True,
                      header=False,
                      subconfig=False),
        )

        self.assertEqual(actual, self.output_grouped)
        mock_print.assert_called_once_with(self.output_grouped)
Example #7
0
    def test_run_dir(self, mock_print):
        actual = run(
            self.test_data,
            Config(CONFIG),
            mock.Mock(formatter='grouped',
                      ci=False,
                      print=True,
                      header=False,
                      subconfig=False),
        )

        self.assertIsNone(actual)
        mock_print.assert_has_calls([
            mock.call(self.output_grouped),
        ])
Example #8
0
    def test_run_dir_skipped(self, mock_print):
        config = deepcopy(CONFIG)
        config['exclude'] = ['*/test_data']

        actual = run(
            self.test_data,
            Config(config),
            mock.Mock(formatter='grouped',
                      ci=False,
                      print=True,
                      header=False,
                      subconfig=False),
        )

        self.assertIsNone(actual)
        mock_print.assert_not_called()
Example #9
0
 def test_run_dir_ci(self, mock_print):
     with self.assertRaises(CIFailure):
         consume(
             run(
                 self.test_data,
                 Config(CONFIG),
                 mock.Mock(
                     formatter="grouped",
                     ci=True,
                     print=True,
                     header=False,
                     subconfig=False,
                     length=None,
                     list=False,
                 ),
             )
         )
Example #10
0
    def test_run_dir(self, mock_print):
        consume(
            run(
                self.test_data,
                Config(CONFIG),
                mock.Mock(formatter='grouped',
                          ci=False,
                          print=True,
                          header=False,
                          subconfig=False,
                          length=None,
                          list=False),
            ))

        mock_print.assert_has_calls([
            mock.call(self.output_grouped),
        ])
Example #11
0
    def test_run_dir_skipped(self, mock_print):
        config = deepcopy(CONFIG)
        config['exclude'] = ['*/test_data']

        consume(
            run(
                self.test_data,
                Config(config),
                mock.Mock(formatter='grouped',
                          ci=False,
                          print=True,
                          header=False,
                          subconfig=False,
                          length=None,
                          list=False),
            ))

        mock_print.assert_not_called()
Example #12
0
    def test_run_file(self, mock_print):
        actual = next(
            run(
                self.test_data / "input.py",
                Config(CONFIG),
                mock.Mock(
                    formatter="grouped",
                    ci=False,
                    print=True,
                    header=False,
                    subconfig=False,
                    length=None,
                    list=False,
                ),
            )
        )

        self.assertEqual(actual, self.output_grouped)
        mock_print.assert_called_once_with(self.output_grouped)
Example #13
0
    def test_run_file_skipped(self, mock_print):
        config = deepcopy(CONFIG)
        config["exclude"] = ["*/test_data/*.py"]

        consume(
            run(
                self.test_data / "input.py",
                Config(config),
                mock.Mock(
                    formatter="grouped",
                    ci=False,
                    print=True,
                    header=False,
                    subconfig=False,
                    length=None,
                    list=False,
                ),
            )
        )

        mock_print.assert_not_called()
Example #14
0
    def test_run_dir_subconfig_invalid(self, mock_print):
        config_file = self.test_data / IMPORTANIZE_CONFIG
        config_file.write_text('invalid json')

        try:
            actual = run(
                self.test_data,
                Config(CONFIG),
                mock.Mock(formatter='grouped',
                          ci=False,
                          print=True,
                          header=False,
                          subconfig=True),
            )

            self.assertIsNone(actual)
            mock_print.assert_has_calls([
                mock.call(self.output_grouped),
            ])

        finally:
            config_file.unlink()
Example #15
0
    def test_find_config_nonfound(self):
        config = Config.find(Path(Path(__file__).root))

        self.assertIsNone(config.path)
Example #16
0
    def test_find_config(self):
        config = Config.find(Path(__file__))

        expected_config = Path(__file__).parent.parent.joinpath(
            IMPORTANIZE_CONFIG)
        self.assertEqual(config.path, expected_config)