Skip to content

Commit

Permalink
Do not intentionally crash in pyre init
Browse files Browse the repository at this point in the history
Reviewed By: shannonzhu

Differential Revision: D23762738

fbshipit-source-id: 361e664ab75bb9034edecf50b4b26272cdaf3e8f
  • Loading branch information
grievejia authored and facebook-github-bot committed Sep 18, 2020
1 parent 222b2b5 commit b0a0af0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
76 changes: 41 additions & 35 deletions client/commands/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from typing import Any, Dict, Optional

from .. import log
from ..exceptions import EnvironmentException
from ..find_directories import (
BINARY_NAME,
CONFIGURATION_FILE,
Expand All @@ -29,6 +28,10 @@
LOG: Logger = logging.getLogger(__name__)


class InitializationException(Exception):
pass


class Initialize(CommandParser):
NAME = "initialize"

Expand Down Expand Up @@ -90,7 +93,7 @@ def _get_configuration(self) -> Dict[str, Any]:
)
)
if not os.path.isfile(binary_path):
raise EnvironmentException(
raise InitializationException(
"Unable to locate binary at `{}`.".format(binary_path)
)
else:
Expand All @@ -103,7 +106,7 @@ def _get_configuration(self) -> Dict[str, Any]:
log.get_input("Unable to locate typeshed, please enter its root: ")
).resolve()
if not typeshed.is_dir():
raise EnvironmentException(
raise InitializationException(
"No typeshed directory found at `{}`.".format(typeshed)
)
configuration["typeshed"] = str(typeshed)
Expand Down Expand Up @@ -141,39 +144,42 @@ def _is_local(self) -> bool:
return find_global_root(Path(".")) is not None

def _run(self) -> None:
is_local = self._is_local()
current_directory = os.getcwd()
configuration_path = os.path.join(current_directory, CONFIGURATION_FILE)
if os.path.isfile(configuration_path):
if is_local:
error = (
"Local configurations must be created in subdirectories of `{}`"
"as it already contains a `.pyre_configuration`.".format(
current_directory
try:
is_local = self._is_local()
current_directory = os.getcwd()
configuration_path = os.path.join(current_directory, CONFIGURATION_FILE)
if os.path.isfile(configuration_path):
if is_local:
error = (
"Local configurations must be created in subdirectories of `{}`"
"as it already contains a `.pyre_configuration`.".format(
current_directory
)
)
else:
error = "A pyre configuration already exists at `{}`.".format(
configuration_path
)
raise InitializationException(error)
if os.path.isfile(configuration_path + ".local"):
raise InitializationException(
"A local pyre configuration already exists at `{}`.".format(
configuration_path + ".local"
)
)
if is_local:
configuration_path = configuration_path + ".local"
configuration = self._get_local_configuration()
else:
error = "A pyre configuration already exists at `{}`.".format(
configuration_path
)
raise EnvironmentException(error)
if os.path.isfile(configuration_path + ".local"):
raise EnvironmentException(
"A local pyre configuration already exists at `{}`.".format(
configuration_path + ".local"
)
configuration = self._get_configuration()

with open(configuration_path, "w+") as configuration_file:
json.dump(configuration, configuration_file, sort_keys=True, indent=2)
configuration_file.write("\n")
LOG.log(
log.SUCCESS,
"Successfully initialized pyre! "
+ "You can view the configuration at `{}`.".format(configuration_path),
)
if is_local:
configuration_path = configuration_path + ".local"
configuration = self._get_local_configuration()
else:
configuration = self._get_configuration()

with open(configuration_path, "w+") as configuration_file:
json.dump(configuration, configuration_file, sort_keys=True, indent=2)
configuration_file.write("\n")
LOG.log(
log.SUCCESS,
"Successfully initialized pyre! "
+ "You can view the configuration at `{}`.".format(configuration_path),
)
except InitializationException as error:
LOG.error(f"{error}")
12 changes: 0 additions & 12 deletions client/commands/tests/initialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
from ... import commands
from ...commands import initialize
from ...commands.initialize import log
from ...exceptions import EnvironmentException
from .command_test import mock_arguments


class InitializeTest(unittest.TestCase):
Expand Down Expand Up @@ -89,16 +87,6 @@ def exists(path):
initialize.Initialize().run()
file().write.assert_has_calls([call("{}"), call("\n")])

def exists(path):
if path.endswith(".pyre_configuration"):
return True
return False

isfile.side_effect = exists
with patch.object(commands.Command, "_call_client"):
with self.assertRaises(EnvironmentException):
initialize.Initialize().run()

with patch.object(commands.Command, "_call_client"), patch.object(
sys, "argv", ["/tmp/pyre/bin/pyre"]
), patch.object(initialize, "find_typeshed", return_value=Path("/tmp")):
Expand Down

0 comments on commit b0a0af0

Please sign in to comment.