def test_api_token_authentication(self): # Given yaml_string = textwrap.dedent("""\ authentication: kind: token api_token: ulysse """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.use_webservice) self.assertEqual(config.auth, APITokenAuth("ulysse")) # Given yaml_string = textwrap.dedent("""\ authentication: api_token: ulysse """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.use_webservice) self.assertEqual(config.auth, APITokenAuth("ulysse"))
def test_files_cache(self): # Given yaml_string = textwrap.dedent("""\ files_cache: "/foo/bar" """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.use_webservice) self.assertEqual(config.repository_cache, "/foo/bar") # Given yaml_string = textwrap.dedent("""\ files_cache: "~/foo/bar/{PLATFORM}" """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.use_webservice) self.assertEqual( config.repository_cache, os.path.expanduser("~/foo/bar/{0}".format(custom_plat)))
def test_max_retries_setup(self): # When config = Configuration() # Then self.assertEqual(config.max_retries, 0) # Given data = StringIO("max_retries = 1") # When config = Configuration.from_file(data) # Then self.assertEqual(config.max_retries, 1) # Given data = StringIO("max_retries = 0") # When config = Configuration.from_file(data) # Then self.assertEqual(config.max_retries, 0) # Given data = StringIO("max_retries = 'a'") # When/Then with self.assertRaises(InvalidConfiguration): Configuration.from_file(data)
class MockedPrint(object): def __init__(self): self.s = StringIO() def __call__(self, *a): self.s.write(" ".join(str(_) for _ in a) + "\n") @property def value(self): return self.s.getvalue()
def test_both_auth_set(self): # Given data = "EPD_auth = '{0}'\napi_token = 'token'" data = StringIO(data.format(FAKE_CREDS)) msg = "Both 'EPD_auth' and 'api_token' set in configuration." \ "\nYou should remove one of those for consistent " \ "behaviour." # When with self.assertWarnsRegex(Warning, msg): Configuration.from_file(data)
def test_use_epd_username(self): data = "EPD_auth = '{0}'".format(FAKE_CREDS) self.assertFalse(_is_using_epd_username(StringIO(data))) data = "#EPD_auth = '{0}'".format(FAKE_CREDS) self.assertFalse(_is_using_epd_username(StringIO(data))) data = textwrap.dedent("""\ #EPD_auth = '{0}' EPD_username = '******' """).format(FAKE_CREDS, FAKE_USER) self.assertTrue(_is_using_epd_username(StringIO(data)))
def test_from_file_complete_combination2(self): # Given if sys.platform == "win32": r_prefix = "C:\\tmp" else: r_prefix = "/tmp" r_repository_cache = r_prefix fp = StringIO( textwrap.dedent("""\ EPD_auth = "{creds}" repository_cache = {prefix!r} prefix = {prefix!r} use_webservice = False store_url = "http://acme.com" use_pypi = True autoupdate = True noapp = True """.format(creds=FAKE_CREDS, prefix=r_prefix))) # When config = Configuration.from_file(fp) # Then self.assertEqual(config.auth, FAKE_AUTH) self.assertSamePath(config.repository_cache, r_repository_cache) self.assertSamePath(config.prefix, r_prefix) self.assertEqual(config.use_webservice, False) self.assertEqual(config.store_url, "http://acme.com") self.assertEqual(config.use_pypi, True) self.assertEqual(config.autoupdate, True) self.assertEqual(config.noapp, True)
def test_simple(self): # Given yaml_string = textwrap.dedent("""\ store_url: "http://acme.com" repositories: - enthought/free - enthought/commercial """) platform = custom_plat r_indices = tuple( (('http://acme.com/repo/enthought/free/{0}/index.json'.format( platform), 'http://acme.com/repo/enthought/free/{0}/index.json'.format( platform)), ('http://acme.com/repo/enthought/commercial/{0}/index.json'. format(platform), 'http://acme.com/repo/enthought/commercial/{0}/index.json'. format(platform)))) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.use_webservice) self.assertEqual(config.store_url, "http://acme.com") self.assertEqual(config.indices, r_indices) self.assertEqual(config.max_retries, 0) self.assertTrue(config.verify_ssl)
def test_epd_auth(self): """ Ensure config auth information is properly set-up when using EPD_auth """ s = StringIO("EPD_auth = '{0}'".format(FAKE_CREDS)) config = Configuration.from_file(s) self.assertEqual(config.auth, FAKE_AUTH)
def test_parse_simple_unsupported_entry(self): # XXX: ideally, we would like something like with self.assertWarns to # check for the warning, but backporting the python 3.3 code to # unittest2 is a bit painful. with mock.patch("enstaller.config.warnings.warn") as m: Configuration.from_file(StringIO("nono = 'le petit robot'")) m.assert_called_with( 'Unsupported configuration setting nono, ignored')
def test_epd_username_wo_keyring(self): """ Ensure config auth correctly reports itself as non configured when using EPD_username but keyring is not available to get password. """ s = StringIO("EPD_username = '******'".format(FAKE_USER)) config = Configuration.from_file(s) self.assertIsNone(config.auth)
def update_index(dir_path, force=False, verbose=False): """ Updates index-depend.txt and index-depend.bz2 in the directory specified. If index-depend.txt already exists, its content (which contains modification time stamps) is used to create the updated file. This can be disabled using the force option. """ txt_path = join(dir_path, 'index-depend.txt') if verbose: print("Updating:", txt_path) if force or not isfile(txt_path): section = {} else: section = parse_index(open(txt_path).read()) # since generating the new data may take a while, we first write to memory # and then write the file afterwards. faux = StringIO() for fn in sorted(os.listdir(dir_path), key=string.lower): if not fn.endswith('.egg'): continue if not is_valid_eggname(fn): print("WARNING: ignoring invalid egg name:", fn) continue path = join(dir_path, fn) if fn in section: spec = parse_data(section[fn], index=True) if spec.get('mtime') == getmtime(path): faux.write('==> %s <==\n' % fn) faux.write(section[fn] + '\n') continue faux.write(index_section(path)) if verbose: sys.stdout.write('.') sys.stdout.flush() if verbose: print() write_txt_bz2(txt_path, faux.getvalue()) faux.close() import enstaller.egg_meta enstaller.egg_meta.update_index(dir_path, force, verbose)
def test_invalid_format(self): # Given yaml_string = textwrap.dedent("""\ repositoriess: - enthought/commercial """) # When/Then with self.assertRaises(InvalidConfiguration): Configuration.from_yaml_filename(StringIO(yaml_string))
def test_epd_username(self): with fake_keyring_context() as mocked_keyring: mocked_keyring.set_password(KEYRING_SERVICE_NAME, FAKE_USER, FAKE_PASSWORD) s = StringIO("EPD_username = '******'".format(FAKE_USER)) config = Configuration.from_file(s) self.assertEqual(config.auth, FAKE_AUTH) self.assertEqual(config.auth._encoded_auth, FAKE_CREDS)
def test_store_kind(self): """ Ensure config auth information is properly set-up when using EPD_auth """ # Given config = Configuration() # When config.update(store_url="http://acme.com") # Then self.assertEqual(config.store_kind, "legacy") self.assertEqual(config.store_url, "http://acme.com") # Given config = Configuration() # When config.update(store_url="brood+http://acme.com") # Then self.assertEqual(config.store_kind, "brood") self.assertEqual(config.store_url, "http://acme.com") # Given s = StringIO("store_url = 'http://acme.com'") # When config = Configuration.from_file(s) # Then self.assertEqual(config.store_kind, "legacy") self.assertEqual(config.store_url, "http://acme.com") # Given s = StringIO("store_url = 'brood+http://acme.com'") # When config = Configuration.from_file(s) # Then self.assertEqual(config.store_kind, "brood") self.assertEqual(config.store_url, "http://acme.com")
def test_default(self): # Given/When config = Configuration.from_yaml_filename(StringIO("")) # Then self.assertFalse(config.use_webservice) self.assertFalse(config.indices, []) self.assertEqual(config.repository_cache, os.path.join(sys.prefix, "LOCAL-REPO")) self.assertEqual(config.store_kind, "brood")
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_max_retries(self): # Given yaml_string = textwrap.dedent("""\ max_retries: 1 """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertEqual(config.max_retries, 1)
def test_invalid_syntax(self): # Given data = "store_url = http://acme.com" r_message = "Could not parse configuration file (invalid python " \ "syntax at line 1: expression 'store_url = " \ "http://acme.com')" # When with self.assertRaises(InvalidConfiguration) as e: Configuration.from_file(StringIO(data)) # Then self.assertMultiLineEqual(str(e.exception), r_message)
def test_parse_simple(self): r_data = {"IndexedRepos": ["http://acme.com/{SUBDIR}"], "webservice_entry_point": "http://acme.com/eggs/{PLATFORM}/"} s = textwrap.dedent("""\ IndexedRepos = [ "http://acme.com/{SUBDIR}", ] webservice_entry_point = "http://acme.com/eggs/{PLATFORM}/" """) data = parse_assignments(StringIO(s)) self.assertEqual(data, r_data)
def test_verify_ssl(self): # Given yaml_string = textwrap.dedent("""\ verify_ssl: True """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertTrue(config.verify_ssl) # Given yaml_string = textwrap.dedent("""\ verify_ssl: False """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.verify_ssl)
def test_verify_ssl_setup(self): # When config = Configuration() # Then self.assertTrue(config.verify_ssl) # Given data = StringIO("verify_ssl = True") # When config = Configuration.from_file(data) # Then self.assertTrue(config.verify_ssl) # Given data = StringIO("verify_ssl = False") # When config = Configuration.from_file(data) # Then self.assertFalse(config.verify_ssl)
def test_unsupported_syntax(self): # Given data = textwrap.dedent("""\ store_url = 'http://acme' store_url += '.com' """) r_message = "Could not parse configuration file (error at line 2: " \ "expression \"store_url += '.com'\" not supported)" # When with self.assertRaises(InvalidConfiguration) as e: Configuration.from_file(StringIO(data)) # Then self.assertMultiLineEqual(str(e.exception), r_message)
def test_basic_authentication(self): # Given yaml_string = textwrap.dedent("""\ authentication: kind: basic auth: {0} """.format(UserPasswordAuth("*****@*****.**", "ulysse")._encoded_auth)) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.use_webservice) self.assertEqual(config.auth, UserPasswordAuth("*****@*****.**", "ulysse"))
def test_simple_authentication(self): # Given yaml_string = textwrap.dedent("""\ authentication: kind: simple username: [email protected] password: ulysse """) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertFalse(config.use_webservice) self.assertEqual(config.auth, UserPasswordAuth("*****@*****.**", "ulysse"))
def test_api_token(self): # Given config = Configuration() # When config.update(auth=APITokenAuth("dummy token")) # Then self.assertEqual(config.auth, APITokenAuth("dummy token")) # Given data = StringIO("api_token = 'yoyo'") # When config = Configuration.from_file(data) # Then self.assertEqual(config.auth, APITokenAuth("yoyo"))
def test_repositories(self): # Given yaml_string = textwrap.dedent("""\ store_url: "http://www.acme.com" repositories: - "enthought/free" - "file:///foo" """) r_repositories = ( "http://www.acme.com/repo/enthought/free/{0}/".format(custom_plat), "file:///foo/".format(custom_plat), ) # When config = Configuration.from_yaml_filename(StringIO(yaml_string)) # Then self.assertEqual(config.indexed_repositories, r_repositories)
def test_simple(self): if sys.platform == "win32": q = "\"" else: q = "" r_cli_entry_point = """\ #!{q}{executable}{q} # This script was created by egginst when installing: # # dummy.egg # if __name__ == '__main__': import sys from dummy import main_cli sys.exit(main_cli()) """.format(executable=sys.executable, q=q) entry_points = """\ [console_scripts] dummy = dummy:main_cli [gui_scripts] dummy-gui = dummy:main_gui """ s = StringIO(entry_points) config = configparser.ConfigParser() config.readfp(s) with mkdtemp() as d: egginst = EggInst("dummy.egg", d) create(egginst, config) if sys.platform == "win32": entry_point = os.path.join(egginst.bin_dir, "dummy-script.py") else: entry_point = os.path.join(egginst.bin_dir, "dummy") self.assertTrue(os.path.exists(entry_point)) with open(entry_point, "rt") as fp: cli_entry_point = fp.read() self.assertMultiLineEqual(cli_entry_point, r_cli_entry_point)
def test_epd_auth_wo_keyring(self): s = StringIO("EPD_auth = '{0}'".format(FAKE_CREDS)) config = Configuration.from_file(s) self.assertEqual(config.auth, FAKE_AUTH)
def test_parse_simple_unsupported_entry(self): # XXX: ideally, we would like to check for the warning, but doing so is # a bit too painful as it has not been backported to unittest2 Configuration.from_file(StringIO("nono = 'le petit robot'"))
def __init__(self): self.s = StringIO()
def test_simple_windows(self): python_executable = "C:\\Python27\\python.exe" pythonw_executable = "C:\\Python27\\pythonw.exe" r_cli_entry_point = """\ #!"{executable}" # This script was created by egginst when installing: # # dummy.egg # if __name__ == '__main__': import sys from dummy import main_cli sys.exit(main_cli()) """.format(executable=python_executable) r_gui_entry_point = """\ #!"{executable}" # This script was created by egginst when installing: # # dummy.egg # if __name__ == '__main__': import sys from dummy import main_gui sys.exit(main_gui()) """.format(executable=pythonw_executable) entry_points = """\ [console_scripts] dummy = dummy:main_cli [gui_scripts] dummy-gui = dummy:main_gui """ s = StringIO(entry_points) config = configparser.ConfigParser() config.readfp(s) with mock.patch("sys.executable", python_executable): with mkdtemp() as d: egginst = EggInst("dummy.egg", d) create(egginst, config) cli_entry_point_path = os.path.join(egginst.bin_dir, "dummy-script.py") gui_entry_point_path = os.path.join(egginst.bin_dir, "dummy-gui-script.pyw") entry_points = [ os.path.join(egginst.bin_dir, "dummy.exe"), os.path.join(egginst.bin_dir, "dummy-gui.exe"), cli_entry_point_path, gui_entry_point_path, ] for entry_point in entry_points: self.assertTrue(os.path.exists(entry_point)) with open(cli_entry_point_path, "rt") as fp: cli_entry_point = fp.read() self.assertMultiLineEqual(cli_entry_point, r_cli_entry_point) with open(gui_entry_point_path, "rt") as fp: gui_entry_point = fp.read() self.assertMultiLineEqual(gui_entry_point, r_gui_entry_point) self.assertEqual(compute_md5(os.path.join(egginst.bin_dir, "dummy.exe")), hashlib.md5(exe_data.cli).hexdigest()) self.assertEqual(compute_md5(os.path.join(egginst.bin_dir, "dummy-gui.exe")), hashlib.md5(exe_data.gui).hexdigest())