def test_find_package_directory(self): info = os.path.join(self.temp_dir, ".click", "info") path = os.path.join(self.temp_dir, "file") Click.ensuredir(info) touch(path) pkgdir = Click.find_package_directory(path) self.assertEqual(self.temp_dir, pkgdir)
def test_build_excludes_dot_click(self): scratch = self._make_scratch_dir() touch(os.path.join(scratch, ".click", "evil-file")) path = self.builder.build(self.temp_dir) extract_path = os.path.join(self.temp_dir, "extract") subprocess.check_call(["dpkg-deb", "-x", path, extract_path]) self.assertEqual([], os.listdir(extract_path))
def test_find_on_path_present_executable(self): bin_dir = os.path.join(self.temp_dir, "bin") program = os.path.join(bin_dir, "program") touch(program) os.chmod(program, 0o755) os.environ["PATH"] = bin_dir self.assertTrue(self.mod.find_on_path("program"))
def test_packages_all_ignores_non_directory(self): os.makedirs(os.path.join(self.temp_dir, "a", "1.0")) touch(os.path.join(self.temp_dir, "file")) self.assertEqual([ ("a", "1.0", os.path.join(self.temp_dir, "a", "1.0")), ], self._installed_packages_tuplify( self.db.get_packages(all_versions=True)))
def _make_ownership_test(self): path = os.path.join(self.temp_dir, "a", "1.0") touch(os.path.join(path, ".click", "info", "a.manifest")) os.symlink("1.0", os.path.join(self.temp_dir, "a", "current")) user_path = os.path.join(self.temp_dir, ".click", "users", "test-user", "a") os.makedirs(os.path.dirname(user_path)) os.symlink(path, user_path) touch(os.path.join(self.temp_dir, ".click", "log"))
def test_build_ignore_pattern(self): scratch = self._make_scratch_dir() touch(os.path.join(scratch, "build", "foo.o")) self.builder.add_file(scratch, "/") self.builder.add_ignore_pattern("build") path = self.builder.build(self.temp_dir) extract_path = os.path.join(self.temp_dir, "extract") subprocess.check_call(["dpkg-deb", "-x", path, extract_path]) self.assertEqual([], os.listdir(extract_path))
def test_get_frameworks_not_directory(self): with self.run_in_subprocess("click_get_frameworks_dir") as (enter, preloads): enter() path = os.path.join(self.temp_dir, "file") touch(path) preloads["click_get_frameworks_dir"].side_effect = ( lambda: self.make_string(path)) self.assertEqual([], Click.Framework.get_frameworks())
def test_get_frameworks_ignores_other_files(self): with self.run_in_subprocess("click_get_frameworks_dir") as (enter, preloads): enter() frameworks_dir = os.path.join(self.temp_dir, "frameworks") Click.ensuredir(frameworks_dir) touch(os.path.join(frameworks_dir, "file")) preloads["click_get_frameworks_dir"].side_effect = ( lambda: self.make_string(frameworks_dir)) self.assertEqual([], Click.Framework.get_frameworks())
def test_new_db_not_directory(self): with self.run_in_subprocess("click_get_db_dir", "g_get_user_name") as (enter, preloads): enter() path = os.path.join(self.temp_dir, "file") touch(path) preloads["click_get_db_dir"].side_effect = ( lambda: self.make_string(path)) self.assertRaisesFileError(GLib.FileError.NOTDIR, Click.User.for_user, None, None)
def test_build(self): self.use_temp_dir() scratch = os.path.join(self.temp_dir, "scratch") touch(os.path.join(scratch, "bin", "foo")) touch(os.path.join(scratch, ".git", "config")) touch(os.path.join(scratch, "foo.so")) touch(os.path.join(scratch, "build", "meep.goah")) with mkfile(os.path.join(scratch, "manifest.json")) as f: json.dump( { "name": "com.example.test", "version": "1.0", "maintainer": "Foo Bar <*****@*****.**>", "title": "test title", "architecture": "all", "framework": "ubuntu-sdk-13.10", }, f) # build() overrides this back to 0o644 os.fchmod(f.fileno(), 0o600) self.builder.add_file(scratch, "./") self.builder.add_ignore_pattern("build") path = os.path.join(self.temp_dir, "com.example.test_1.0.tar.gz") self.assertEqual(path, self.builder.build(self.temp_dir)) self.assertTrue(os.path.exists(path)) with tarfile.open(path, mode="r:gz") as tar: self.assertCountEqual( [".", "./bin", "./bin/foo", "./manifest.json"], tar.getnames()) self.assertTrue(tar.getmember("./bin/foo").isfile())
def test_gc_ignores_non_directory(self): with self.run_in_subprocess("getpwnam") as (enter, preloads): enter() preloads["getpwnam"].side_effect = ( lambda name: self.make_pointer(Passwd(pw_uid=1, pw_gid=1))) a_path = os.path.join(self.temp_dir, "a", "1.0") a_manifest_path = os.path.join(a_path, ".click", "info", "a.manifest") with mkfile(a_manifest_path) as manifest: json.dump({"hooks": {"a-app": {}}}, manifest) a_user_path = os.path.join(self.temp_dir, ".click", "users", "test-user", "a") os.makedirs(os.path.dirname(a_user_path)) os.symlink(a_path, a_user_path) touch(os.path.join(self.temp_dir, "file")) self.db.gc() self.assertTrue(os.path.exists(a_path))
def test_world_readable(self, mock_package_install_hooks): with self.run_in_subprocess("click_get_frameworks_dir") as (enter, preloads): enter() owner_only_file = os.path.join(self.temp_dir, "owner-only-file") touch(owner_only_file) os.chmod(owner_only_file, stat.S_IRUSR | stat.S_IWUSR) owner_only_dir = os.path.join(self.temp_dir, "owner-only-dir") os.mkdir(owner_only_dir, stat.S_IRWXU) path = self.make_fake_package( control_fields={ "Package": "test-package", "Version": "1.1", "Architecture": "all", "Maintainer": "Foo Bar <*****@*****.**>", "Description": "test", "Click-Version": "0.2", }, manifest={ "name": "test-package", "version": "1.1", "framework": "ubuntu-sdk-13.10", }, control_scripts={"preinst": static_preinst}, data_files={ "world-readable-file": owner_only_file, "world-readable-dir": owner_only_dir, }) root = os.path.join(self.temp_dir, "root") db = Click.DB() db.add(root) installer = ClickInstaller(db) self._setup_frameworks(preloads, frameworks=["ubuntu-sdk-13.10"]) with mock_quiet_subprocess_call(): installer.install(path) inst_dir = os.path.join(root, "test-package", "current") self.assertEqual( stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH, self._get_mode(os.path.join(inst_dir, "world-readable-file"))) self.assertEqual( stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH, self._get_mode(os.path.join(inst_dir, "world-readable-dir")))
def test_remove_does_not_follow_symlinks_inside_dir(self): """ Ensure that symlinks that are inside a user-data dir get removed, but that the symlink targets are not touched """ fake_cache_dir = os.path.join(os.environ["XDG_CACHE_HOME"], self.appname) os.makedirs(fake_cache_dir) # setup symlink inside the config dir that that points # to a file that is outside of the config dir canary = os.path.join(self.temp_dir, "canary") touch(canary) os.symlink(canary, os.path.join(fake_cache_dir, "canary")) self.assertTrue(os.path.islink(os.path.join(fake_cache_dir, "canary"))) self.registry.remove(self.appname) # ensure that the cache dir is gone self.assertFalse(os.path.exists(fake_cache_dir)) # ... but the canary file is still there self.assertTrue(os.path.exists(canary))
def make_fake_package(self, control_fields=None, manifest=None, control_scripts=None, data_files=None): """Build a fake package with given contents.""" control_fields = {} if control_fields is None else control_fields control_scripts = {} if control_scripts is None else control_scripts data_files = {} if data_files is None else data_files data_dir = os.path.join(self.temp_dir, "fake-package") control_dir = os.path.join(self.temp_dir, "DEBIAN") with mkfile(os.path.join(control_dir, "control")) as control: for key, value in control_fields.items(): print('%s: %s' % (key.title(), value), file=control) print(file=control) if manifest is not None: with mkfile(os.path.join(control_dir, "manifest")) as f: json.dump(manifest, f) print(file=f) for name, contents in control_scripts.items(): with mkfile(os.path.join(control_dir, name)) as script: script.write(contents) Click.ensuredir(data_dir) for name, path in data_files.items(): Click.ensuredir(os.path.dirname(os.path.join(data_dir, name))) if path is None: touch(os.path.join(data_dir, name)) elif os.path.isdir(path): shutil.copytree(path, os.path.join(data_dir, name)) else: shutil.copy2(path, os.path.join(data_dir, name)) package_path = '%s.click' % data_dir ClickBuilder()._pack(self.temp_dir, control_dir, data_dir, package_path) return package_path
def test_listdir_oserror(self): not_dir = os.path.join(self.temp_dir, "file") touch(not_dir) self.assertRaises(OSError, osextras.listdir_force, not_dir)
def test_build(self): self.use_temp_dir() scratch = os.path.join(self.temp_dir, "scratch") with mkfile(os.path.join(scratch, "bin", "foo")) as f: f.write("test /bin/foo\n") os.symlink("foo", os.path.join(scratch, "bin", "bar")) touch(os.path.join(scratch, ".git", "config")) with mkfile(os.path.join(scratch, "toplevel")) as f: f.write("test /toplevel\n") os.symlink("file-does-not-exist", os.path.join(scratch, "broken-symlink")) with mkfile(os.path.join(scratch, "manifest.json")) as f: json.dump( { "name": "com.example.test", "version": "1.0", "maintainer": "Foo Bar <*****@*****.**>", "title": "test title", "architecture": "all", "framework": "ubuntu-sdk-13.10", }, f) # build() overrides this back to 0o644 os.fchmod(f.fileno(), 0o600) self.builder.add_file(scratch, "/") path = os.path.join(self.temp_dir, "com.example.test_1.0_all.click") self.assertEqual(path, self.builder.build(self.temp_dir)) self.assertTrue(os.path.exists(path)) for key, value in ( ("Package", "com.example.test"), ("Version", "1.0"), ("Click-Version", "0.4"), ("Architecture", "all"), ("Maintainer", "Foo Bar <*****@*****.**>"), ("Description", "test title"), ): self.assertEqual(value, self.extract_field(path, key)) self.assertNotEqual("", self.extract_field(path, "Installed-Size")) control_path = os.path.join(self.temp_dir, "control") subprocess.check_call(["dpkg-deb", "-e", path, control_path]) manifest_path = os.path.join(control_path, "manifest") self.assertEqual(0o644, stat.S_IMODE(os.stat(manifest_path).st_mode)) with open(os.path.join(scratch, "manifest.json")) as source, \ open(manifest_path) as target: source_json = json.load(source) target_json = json.load(target) self.assertNotEqual("", target_json["installed-size"]) del target_json["installed-size"] self.assertEqual(source_json, target_json) with open(os.path.join(control_path, "md5sums")) as md5sums: self.assertRegex( md5sums.read(), r"^" r"eb774c3ead632b397d6450d1df25e001 bin/bar\n" r"eb774c3ead632b397d6450d1df25e001 bin/foo\n" r"49327ce6306df8a87522456b14a179e0 toplevel\n" r"$") with open(os.path.join(control_path, "preinst")) as preinst: self.assertEqual(static_preinst, preinst.read()) contents = subprocess.check_output(["dpkg-deb", "-c", path], universal_newlines=True) self.assertRegex(contents, r"^drwxr-xr-x root/root 0 .* \./\n") self.assertRegex( contents, "\nlrwxrwxrwx root/root 0 .* \./bin/bar -> foo\n") self.assertRegex(contents, "\n-rw-r--r-- root/root 14 .* \./bin/foo\n") self.assertRegex(contents, "\n-rw-r--r-- root/root 15 .* \./toplevel\n") extract_path = os.path.join(self.temp_dir, "extract") subprocess.check_call(["dpkg-deb", "-x", path, extract_path]) for rel_path in ( os.path.join("bin", "foo"), "toplevel", ): with open(os.path.join(scratch, rel_path)) as source, \ open(os.path.join(extract_path, rel_path)) as target: self.assertEqual(source.read(), target.read()) self.assertTrue( os.path.islink(os.path.join(extract_path, "bin", "bar"))) self.assertEqual("foo", os.readlink(os.path.join(extract_path, "bin", "bar")))
def test_ensuredir_error(self): path = os.path.join(self.temp_dir, "file") touch(path) self.assertRaisesFileError(mock.ANY, self.mod.ensuredir, path)
def test_dir_read_name_directory_present(self): new_dir = os.path.join(self.temp_dir, "dir") touch(os.path.join(new_dir, "file")) d = Click.Dir.open(new_dir, 0) self.assertEqual("file", d.read_name()) self.assertIsNone(d.read_name())
def test_read_not_directory(self): path = os.path.join(self.temp_dir, "file") touch(path) db = Click.DB() self.assertRaisesFileError(GLib.FileError.NOTDIR, db.read, db_dir=path)
def test_ensuredir_oserror(self): path = os.path.join(self.temp_dir, "file") touch(path) self.assertRaises(OSError, self.mod.ensuredir, path)
def test_dir_open_error(self): not_dir = os.path.join(self.temp_dir, "file") touch(not_dir) self.assertRaisesFileError(GLib.FileError.NOTDIR, Click.Dir.open, not_dir, 0)
def test_symlink_file_present(self): path = os.path.join(self.temp_dir, "link") touch(path) self.mod.symlink_force("source", path) self.assertTrue(os.path.islink(path)) self.assertEqual("source", os.readlink(path))
def test_unlink_file_present(self): path = os.path.join(self.temp_dir, "file") touch(path) self.mod.unlink_force(path) self.assertFalse(os.path.exists(path))
def test_find_on_path_present_not_executable(self): bin_dir = os.path.join(self.temp_dir, "bin") touch(os.path.join(bin_dir, "program")) os.environ["PATH"] = bin_dir self.assertFalse(self.mod.find_on_path("program"))
def test_listdir_directory_present(self): new_dir = os.path.join(self.temp_dir, "dir") touch(os.path.join(new_dir, "file")) self.assertEqual(["file"], osextras.listdir_force(new_dir))
def test_init_rejects_readonly_fileobj(self): path = os.path.join(self.temp_dir, "foo.a") touch(path) with open(path, "rb") as fileobj: self.assertRaises(ValueError, ArFile, fileobj=fileobj)