Exemple #1
0
def _refactor(
    files: Sequence[pathlib.Path],
    config_path: Optional[pathlib.Path],
    debug: bool,
) -> None:
    """Subcommand to refactor code."""
    # In Python 3.8 for Mac OS the default was changed from fork to spawn,
    # however that brings lot of undesired effects. For example the Pickling
    # of many structures, which is fragile and the errors are undecipherable.
    # Aditionally it means, that logging and global state won't be preserved.
    # Spawning is also slower.
    # Note that it should be safe to use fork as we don't have any
    # multi-threaded code.
    if (platform.system() == "Darwin"
            and multiprocessing.get_start_method(allow_none=True) != "fork"):
        multiprocessing.set_start_method("fork")
    level = "DEBUG" if debug else "WARNING"
    logger.remove()
    logger.add(
        sys.stderr,
        level=level,
        format="{level}\t{elapsed}\t{message}",
        enqueue=True,
    )
    logger.enable("craftier")

    if debug:
        performance.enable()

    if not files:
        click.secho("No path provided. Nothing to do 💤", bold=True)
        return
    try:
        craftier_config = config.load(config_path)
        logger.debug("Config path: {}", craftier_config.path)
        result = refactor.run(craftier_config, files)
        _fix_summary(result)
    except (config.InvalidConfigError, refactor.Error) as e:
        print(e)
        raise click.exceptions.Exit(1)
    except KeyboardInterrupt:
        raise click.Abort("Aborted by user") from None

    if debug:
        performance.disable()
Exemple #2
0
 def test_disable(self) -> None:
     performance.enable()
     performance.disable()
     self.assertIsNone(performance._config.file)
Exemple #3
0
 def test_read_empty(self) -> None:
     performance.enable()
     self.assertEqual(performance.read(), [])
Exemple #4
0
 def test_write_read_write_read(self) -> None:
     performance.enable()
     performance.write({"foo": 1})
     self.assertEqual(performance.read(), [{"foo": 1}])
     performance.write({"bar": 2})
     self.assertEqual(performance.read(), [{"foo": 1}, {"bar": 2}])
Exemple #5
0
 def test_enable_twice(self) -> None:
     performance.enable()
     with self.assertRaises(performance.PerformanceError):
         performance.enable()