def test_401_index_handling(self): self.maxDiff = None # Given repo = "http://acme.com/repo/ets/" config = Configuration() config.update(use_webservice=False, indexed_repositories=[repo]) config.update(auth=("nono", "le petit robot")) config.write(self.config) url = repo + "index.json" responses.add(responses.HEAD, url, status=401) error_message = textwrap.dedent("""\ Could not authenticate as 'nono' (server answered: 'Invalid credentials') Ensure your username and password are correct. You can change your authentication details with 'enpkg --userpass'. """) # When with use_given_config_context(self.config): with mock_print() as m: with self.assertRaises(SystemExit): main(["dummy_requirement"]) # Then self.assertMultiLineEqual(m.value, error_message)
def test_invalid_certificate_config(self): self.maxDiff = None # Given url = "http://acme.com" config = Configuration() config.update(store_url=url) config.update(auth=("nono", "le petit robot")) config.write(self.config) responses.add_callback(responses.GET, re.compile(url + "/*"), self._dummy_ssl_error_callback) error_message = "To connect to 'acme.com' insecurely, add the `-k` " \ "flag to enpkg command" # When with use_given_config_context(self.config): with mock_print() as m: with self.assertRaises(SystemExit): main(["--config"]) # Then last_line = m.value.splitlines()[-1] self.assertMultiLineEqual(last_line, error_message)
def test_invalid_certificate_dont_webservice(self): self.maxDiff = None # Given url = "http://acme.com" config = Configuration() config.update(use_webservice=False, indexed_repositories=["http://acme.com/repo/"]) config.update(auth=("nono", "le petit robot")) config.write(self.config) def callback(request): msg = "Dummy SSL error" raise requests.exceptions.SSLError(msg, request=request) responses.add_callback(responses.HEAD, re.compile(url + "/*"), callback) error_message = textwrap.dedent("""\ SSL error: Dummy SSL error To connect to 'acme.com' insecurely, add the `-k` flag to enpkg command """) # When with use_given_config_context(self.config): with mock_print() as m: with self.assertRaises(SystemExit): main(["dummy_requirement"]) # Then self.assertMultiLineEqual(m.value, error_message)
def test_print_version(self): # XXX: this is lousy test: we'd like to at least ensure we're printing # the correct version, but capturing the stdout is a bit tricky. Once # we replace print by proper logging, we should be able to do better. try: main(["--version"]) except SystemExit as e: self.assertEqual(e.code, 0)
def test_non_existing_config_path(self, install_req): # Given args = ["--config-path=config.yaml", "foo"] # When/Then with self.assertRaises(SystemExit) as exc: main(args) self.assertEqual(exception_code(exc), -1)
def test_non_existing_config_path(self, install_req): # Given dummy_path = os.path.abspath(os.path.join("yola", "config.yaml")) args = ["--config-path={0}".format(dummy_path), "foo"] # When/Then with self.assertRaises(SystemExit) as exc: main(args) self.assertEqual(exception_code(exc), -1)
def test_user_valid_config(self, install_req): # Given args = ["--user", "foo"] # When with mock.patch("enstaller.main.check_prefixes") as m: main(args) # Then m.assert_called_with([site.USER_BASE, sys.prefix])
def test_enstaller_in_req(self): r_output = textwrap.dedent("""\ Enstaller has been updated. Please re-run your previous command. """) with mock_print() as m: with mock.patch("enstaller.main.inplace_update"): main(["enstaller"]) self.assertMultiLineEqual(m.value, r_output)
def test_user_invalid_config(self, install_req): # Given r_msg = "Using the --user option, but your PYTHONPATH is not setup " \ "accordingly" args = ["--user", "foo"] # When/Then with self.assertWarnsRegex(Warning, r_msg): main(args)
def test_setup_proxy(self, install_req): # Given args = ["--proxy=http://acme.com:3128", "foo"] # When with mock.patch("enstaller.main.setup_proxy_or_die") as m: main(args) # Then m.assert_called() self.assertEqual(m.call_args[0][1], "http://acme.com:3128")
def test_max_retries(self, install_req): # Given args = ["--max-retries", "42"] # When with mock.patch("enstaller.main.dispatch_commands_with_enpkg") as m: main(args) config = m.call_args[0][2] # Then m.assert_called() self.assertEqual(config.max_retries, 42)
def test_home_expand(self, install_req): # Given path = "~/config.yaml" args = ["--config-path=~/config.yaml", "foo"] r_path = os.path.expanduser(path) # When with mock.patch("enstaller.main.Configuration.from_yaml_filename", return_value=Configuration()) \ as mocked_factory: with self.assertRaises(SystemExit) as exc: main(args) self.assertEqual(exception_code(exc), -1) # Then mocked_factory.assert_called_with(r_path)
def test_config_path_missing_auth(self, install_req): # Given path = os.path.join(self.prefix, "config.yaml") with open(path, "wt") as fp: fp.write("") args = ["--config-path=" + path, "foo"] r_msg = "Authentication missing from {0!r}\n".format(path) # When with mock_print() as m: with self.assertRaises(SystemExit) as exc: main(args) # Then self.assertEqual(exception_code(exc), -1) self.assertEqual(m.value, r_msg)
def test_simple(self): # Given path = os.path.join(self.prefix, "enstaller.yaml") responses.add(responses.POST, "http://acme.com/api/v0/json/auth/tokens/auth", body=json.dumps({"token": "dummy token"})) with open(path, "wt") as fp: fp.write(textwrap.dedent("""\ store_url: "http://acme.com" authentication: kind: "simple" username: "******" password: "******" """)) # When # Then No exception main(["-s", "numpy", "-c", path])
def test_verbose_flag(self, install_req): # Given args = ["foo"] # When with mock.patch("enstaller.logging.basicConfig") as m: main(args) # Then m.assert_called() self.assertEqual(m.call_args[1]["level"], logging.WARN) # Given args = ["foo", "-v"] # When with mock.patch("enstaller.logging.basicConfig") as m: main(args) # Then m.assert_called() self.assertEqual(m.call_args[1]["level"], logging.INFO) # Given args = ["foo", "-vv"] # When with mock.patch("enstaller.main.http_client.HTTPConnection") \ as fake_connection: with mock.patch("enstaller.logging.basicConfig") as m: main(args) # Then fake_connection.assert_not_called() m.assert_called() self.assertEqual(m.call_args[1]["level"], logging.DEBUG) # Given args = ["foo", "-vvv"] # When with mock.patch("enstaller.main.http_client.HTTPConnection") \ as fake_connection: with mock.patch("enstaller.logging.basicConfig") as m: main(args) # Then fake_connection.assert_called() self.assertEqual(fake_connection.debuglevel, 1) m.assert_called() self.assertEqual(m.call_args[1]["level"], logging.DEBUG)
def test_quiet(self, install_req): # Given args = ["foo"] # When with mock.patch("sys.stdout", new=StringIO()) as m: main(args) # Then self.assertNotEqual(m.getvalue(), "") # Given args = ["foo", "--quiet"] # When with mock.patch("sys.stdout", new=StringIO()) as m: main(args) # Then self.assertEqual(m.getvalue(), "")
def test_simple(self): # Given path = os.path.join(self.prefix, "enstaller.yaml") responses.add(responses.POST, "http://acme.com/api/v0/json/auth/tokens/auth", body=json.dumps({"token": "dummy token"})) with open(path, "wt") as fp: fp.write( textwrap.dedent("""\ store_url: "http://acme.com" authentication: kind: "simple" username: "******" password: "******" """)) # When # Then No exception main(["-s", "numpy", "-c", path])
def test_enpkg_req_with_valid_auth(self): """ Ensure 'enpkg req' authenticate as expected """ with open(self.config, "w") as fp: fp.write("EPD_auth = '{0}'".format(FAKE_CREDS)) json_data = { "is_authenticated": True, "first_name": "nono", "last_name": "le petit robot", "has_subscription": True, "subscription_level": "free" } responses.add(responses.GET, "https://api.enthought.com/accounts/user/info/", body=json.dumps(json_data), status=200, content_type='application/json') with use_given_config_context(self.config): main(["nono"])
def test_no_autoupdate(self, install_req): # Given args = ["foo"] # When with mock.patch("enstaller.main.update_enstaller") as m: main(args) # Then self.assertTrue(m.called) # Given args = ["--no-autoupdate", "foo"] # When with mock.patch("enstaller.main.update_enstaller") as m: main(args) # Then self.assertFalse(m.called) # Given args = ["foo"] config = Configuration() config.update(autoupdate=False, auth=("dummy", "dummy")) mock_config = mock.Mock() mock_config.return_value = config mock_config.from_file.return_value = config # When with mock.patch("enstaller.main.Configuration", mock_config): with mock.patch("enstaller.main.update_enstaller") as m: main(args) # Then self.assertFalse(m.called)
def test_install_epd(self): with mock.patch("enstaller.main.epd_install_confirm") as m: main(["epd"]) self.assertTrue(m.called)
def test_install_numpy(self): main(["numpy"])
def test_updated_enstaller_in_req(self): with mock_print() as m: with mock.patch("enstaller.main.install_req"): main(["enstaller"]) self.assertMultiLineEqual(m.value, "")
def test_updated_enstaller(self, logger): with mock.patch("enstaller.main.install_req"): main([""]) logger.info.assert_called_with('prefix: %r', os.path.normpath(sys.prefix))
#!"C:\Program Files (x86)\IronPython 2.7\ipy.exe" # This script was created by egginst when installing: # # ironpkg-1.0.0-1.egg # if __name__ == '__main__': import sys from enstaller.main import main sys.exit(main())
def test_remove(self): with mock.patch("enstaller.main.Enpkg.execute"): main(["--remove", "numpy"])
def test_install_epd_and_other(self): with mock.patch("enstaller.main.epd_install_confirm"): with mock.patch("enstaller.main.install_req"): with self.assertRaises(SystemExit) as e: main(["epd", "numpy"]) self.assertNotEqual(exception_code(e), 0)
def test_remove_epd_fails(self): with mock.patch("enstaller.main.epd_install_confirm"): with mock.patch("enstaller.main.install_req"): with self.assertRaises(SystemExit) as e: main(["--remove", "epd"]) self.assertNotEqual(exception_code(e), 0)
def test_help_runs_and_exits_correctly(self): try: main(["--help"]) except SystemExit as e: self.assertEqual(e.code, 0)
def test_print_env(self): try: main(["--env"]) except SystemExit as e: self.assertEqual(e.code, 0)