def test_replace_old_bin_path(self): """Should delete the old bin path and move new into it's place.""" with get_temp_dir() as installed, get_temp_dir() as staging: self.update_info.bin_path = installed self.update_info.bin_new_path = staging lcp = os.path.join(staging, ".last_check") self.updater._write_last_check(file_path=lcp, content=self.utc_now) self.updater._replace_old_bin_path(update_info=self.update_info) self.assertFalse(os.path.exists(staging)) self.assertEqual(1, len(os.listdir(installed))) self.assertTrue(os.path.exists(installed))
def test_unzip_extract_file__ok(self): """Should extract the specified item to the target output path.""" with get_temp_dir() as temp_dir, ZipFile(self.zip_file, mode="r") as zip_file: zip_item = zip_file.infolist()[0] file_out_path = os.path.join(temp_dir, "validate") self.updater._unzip_extract_file(open_zip_file=zip_file, zip_item=zip_item, file_out_path=file_out_path) self.assertTrue(os.path.exists(file_out_path))
def test_unzip(self): """Should unzip the file to the locations in the bin_path map.""" with get_temp_dir() as temp_dir: self.updater._unzip(update_info=self.update_info, file_path=self.zip_file, out_path=temp_dir) dir_list = [ os.path.join(r, f) for r, _, fs in os.walk(temp_dir) for f in fs ] self.assertEqual(3, len(dir_list))
def test_unzip_find_zip_jobs__not_found_raises(self): """Should raise an error if zip jobs isn't same length as search.""" bin_paths = [(".non_existent", ".non_existent")] with get_temp_dir() as temp_dir, ZipFile( self.zip_file, mode="r") as zip_file, self.assertRaises(PyXFormError) as ctx: self.updater._unzip_find_jobs(open_zip_file=zip_file, bin_paths=bin_paths, out_path=temp_dir) self.assertIn("1 zip job files, found: 0", str(ctx.exception))
def test_unzip_find_zip_jobs__ok_real_dupes(self): """Should return a list of zip jobs same length as search.""" with get_temp_dir() as temp_dir, ZipFile(self.zip_file_dupes, mode="r") as zip_file: bin_paths = self.updater._get_bin_paths( update_info=self.update_info, file_path=self.zip_file_dupes) jobs = self.updater._unzip_find_jobs(open_zip_file=zip_file, bin_paths=bin_paths, out_path=temp_dir) self.assertEqual(3, len(jobs.keys())) self.assertTrue(list(jobs.keys())[0].startswith(temp_dir))
def test_unzip_extract_file__bad_crc_raises(self): """Should raise an error if the zip file CRC doesn't match.""" with get_temp_dir() as temp_dir, ZipFile( self.zip_file, mode="r") as zip_file, self.assertRaises(BadZipFile) as ctx: zip_item = [ x for x in zip_file.infolist() if x.filename.endswith("validate") ][0] zip_item.CRC = 12345 file_out_path = os.path.join(temp_dir, "validate") self.updater._unzip_extract_file(open_zip_file=zip_file, zip_item=zip_item, file_out_path=file_out_path) self.assertIn("Bad CRC-32 for file", str(ctx.exception))
def test_check__ok(self): """Should show a message with relevant info.""" new = self.utc_now - timedelta(minutes=15.0) with get_temp_dir() as mod_root: update_info = get_update_info(check_ok=True, mod_root=mod_root) update_info.latest_path = self.install_fake_old self.updater._write_last_check( file_path=update_info.last_check_path, content=new) self.updater.update(update_info=update_info, file_name="linux.zip") self.updater.check(update_info=update_info) info = capture_handler.watcher.output["INFO"][1] self.assertIn("Check success!", info) self.assertIn("installed release appears to work", info)
def test_install__add_executable_mode(self): """Should add executable mode to the new bin file's modes.""" self.update_info.latest_path = self.install_fake new = self.utc_now - timedelta(minutes=15.0) with get_temp_file() as temp_check, get_temp_dir() as temp_dir: self.updater._write_last_check(file_path=temp_check, content=new) self.update_info.last_check_path = temp_check self.update_info.bin_new_path = temp_dir self.updater._install(update_info=self.update_info, file_name="linux.zip") bin_new = os.path.join(temp_dir, self.update_info.validator_basename) bin_new_stat_mode = os.stat(bin_new).st_mode self.assertEqual(bin_new_stat_mode & S_IXUSR, S_IXUSR) self.assertEqual(bin_new_stat_mode & S_IXGRP, S_IXGRP)
def test_update__not_installed__ok(self): """Should install and show a message with relevant info.""" new = self.utc_now - timedelta(minutes=15.0) with get_temp_dir() as mod_root: update_info = get_update_info(check_ok=True, mod_root=mod_root) update_info.latest_path = self.install_fake self.updater._write_last_check( file_path=update_info.last_check_path, content=new) expected_path = os.path.join(update_info.bin_path, "validate") self.assertFalse(os.path.exists(expected_path)) self.updater.update(update_info=update_info, file_name="linux.zip") self.assertTrue(os.path.exists(expected_path)) info = capture_handler.watcher.output["INFO"][0] self.assertIn("Update success!", info)
def test_check__fail__install_check(self): """Should raise an error if the installation check fails.""" new = self.utc_now - timedelta(minutes=15.0) with get_temp_dir() as mod_root, self.assertRaises( PyXFormError) as ctx: update_info = get_update_info(check_ok=True, mod_root=mod_root) update_info.latest_path = self.install_fake_old self.updater._write_last_check( file_path=update_info.last_check_path, content=new) self.updater.update(update_info=update_info, file_name="linux.zip") update_info.install_check = install_check_fail self.updater.check(update_info=update_info) error = str(ctx.exception) self.assertIn("Check failed!", error) self.assertIn("installed release does not appear to work", error)
def test_update__installed__fail__already_latest(self): """Should stop install and raise an error with relevant info.""" new = self.utc_now - timedelta(minutes=15.0) with get_temp_dir() as mod_root, self.assertRaises( PyXFormError) as ctx: update_info = get_update_info(check_ok=True, mod_root=mod_root) update_info.latest_path = self.install_fake self.updater._write_last_check( file_path=update_info.last_check_path, content=new) self.updater.update(update_info=update_info, file_name="linux.zip") update_info.latest_path = self.install_fake self.updater.update(update_info=update_info, file_name="linux.zip") error = str(ctx.exception) self.assertIn("Update failed!", error) self.assertIn("installed release appears to be the latest", error)
def test_update__not_installed__fail__install_check(self): """Should stop install and raise an error with relevant info.""" new = self.utc_now - timedelta(minutes=15.0) with get_temp_dir() as mod_root, self.assertRaises( PyXFormError) as ctx: update_info = get_update_info(check_ok=False, mod_root=mod_root) update_info.latest_path = self.install_fake self.updater._write_last_check( file_path=update_info.last_check_path, content=new) self.assertFalse(os.path.exists(update_info.bin_path)) self.updater.update(update_info=update_info, file_name="linux.zip") self.assertFalse(os.path.exists(update_info.bin_path)) self.assertTrue(os.path.exists(update_info.bin_new_path)) error = str(ctx.exception) self.assertIn("Update failed!", error) self.assertIn("latest release does not appear to work", error)
def test_install__ok(self): """Should install the latest release and return it's info dict.""" self.update_info.latest_path = self.install_fake new = self.utc_now - timedelta(minutes=15.0) with get_temp_file() as temp_check, get_temp_dir() as temp_dir: self.updater._write_last_check(file_path=temp_check, content=new) self.update_info.last_check_path = temp_check self.update_info.bin_new_path = temp_dir installed = self.updater._install(update_info=self.update_info, file_name="linux.zip") dir_list = [ os.path.join(r, f) for r, _, fs in os.walk(temp_dir) for f in fs ] self.assertEqual(5, len(dir_list)) latest = self.updater._read_json(file_path=self.install_fake) self.assertDictEqual(latest, installed)
def test_xls2xform_convert__e2e_row_with_no_column_value(self): """Programmatically-created XLSX files may have rows without column values""" md = """ | survey | | | | | | | type | name | label | hint | | | text | state | State | | | | text | city | City | A hint | """ wb = md_table_to_workbook(md) with get_temp_dir() as tmp: wb_path = os.path.join(tmp, "empty_cell.xlsx") wb.save(wb_path) wb.close() xls2xform_convert( xlsform_path=wb_path, xform_path=get_xml_path(wb_path), ) xform_path = os.path.join(tmp, "empty_cell.xml") self.assertTrue(os.path.exists(xform_path))
def test_get_temp_dir(self): """Should provide a temp dir that's cleared on exit.""" with get_temp_dir() as temp_dir: self.assertTrue(os.path.exists(temp_dir)) self.assertTrue(os.path.isdir(temp_dir)) self.assertFalse(os.path.exists(temp_dir))