def test_write_cache_file(): from xylem.sources_list import write_cache_file, compute_filename_hash tempdir = tempfile.mkdtemp() filepath = write_cache_file(tempdir, 'foo', {'data': 1}) computed_path = os.path.join(tempdir, compute_filename_hash('foo')) assert os.path.samefile(filepath, computed_path) with open(filepath, 'r') as f: assert {'data': 1} == yaml.load(f.read())
def test_update_sources_list(): from xylem.sources_list import update_sources_list, InvalidData, compute_filename_hash sources_list_dir=get_test_dir() tempdir = tempfile.mkdtemp() # use a subdirectory of test dir to make sure xylem creates the necessary substructure tempdir = os.path.join(tempdir, 'newdir') errors = [] def error_handler(loc, e): errors.append((loc, e)) retval = update_sources_list(sources_list_dir=sources_list_dir, sources_cache_dir=tempdir, error_handler=error_handler) assert retval assert len(retval) == 2, retval # one of our sources is intentionally bad, this should be a softfail assert len(errors) == 1, errors assert errors[0][0].url == 'https://badhostname.willowgarage.com/xylem.yaml' source0, path0 = retval[0] assert source0.origin.endswith('20-default.list'), source0 hash1 = compute_filename_hash(GITHUB_URL) hash2 = compute_filename_hash(BADHOSTNAME_URL) filepath = os.path.join(tempdir, hash1) assert filepath == path0, "%s vs %s"%(filepath, path0) with open(filepath, 'r') as f: data = yaml.load(f) assert 'cmake' in data # verify that cache index exists. contract specifies that even # failed downloads are specified in the index, just in case old # download data is present. with open(os.path.join(tempdir, 'index'), 'r') as f: index = f.read().strip() expected = """#autogenerated by xylem, do not edit. use 'xylem update' instead yaml %s yaml %s python yaml %s ubuntu"""%(GITHUB_URL, GITHUB_PYTHON_URL, BADHOSTNAME_URL) assert expected == index, "\n[%s]\nvs\n[%s]"%(expected, index)