def test_license(self): project_dir = 'license' self.run_snapcraft('strip', project_dir) meta_dir = os.path.join(project_dir, 'snap', 'meta') self.assertThat(meta_dir, DirExists()) license_asset = os.path.join(meta_dir, 'license.txt') self.assertThat(license_asset, FileExists()) self.assertThat(license_asset, FileContains('This is a license.\n'))
def test_snapped_library_takes_precedence_over_system(self): self.run_snapcraft('stage', 'fake-curl-library') self.run_snapcraft(['prime', 'main']) # Verify that, while the binary was primed, no library was pulled in. self.assertThat(os.path.join(self.prime_dir, 'bin', 'main'), FileExists()) self.assertThat(os.path.join(self.prime_dir, 'lib'), Not(DirExists())) self.assertThat(os.path.join(self.prime_dir, 'usr'), Not(DirExists())) # Prime the rest of the way. self.run_snapcraft('prime') # Now verify the lib we got was the one from the snap, not from the # system. self.assertThat(os.path.join(self.prime_dir, 'lib', 'libcurl.so'), FileExists()) self.assertThat(os.path.join(self.prime_dir, 'usr'), Not(DirExists()))
def populate_captures_dir(self, capture_names): # captures dir should not exist on fresh run self.assertThat(self.captures_dir, Not(DirExists())) # create some random files and ensure they get cleaned up os.mkdir(self.captures_dir) for capture_name in capture_names: open(path.join(self.captures_dir, capture_name), 'w').close() self.assertThat(self.captures_dir, DirContains(capture_names))
def test_license(self): project_dir = 'license' self.run_snapcraft('strip', project_dir) hooks_dir = os.path.join(project_dir, 'snap', 'meta', 'hooks') self.assertThat(hooks_dir, DirExists()) license_hook = os.path.join(hooks_dir, 'license') self.assertThat(license_hook, FileExists()) self.assertThat(license_hook, FileContains('This is a license.\n'))
def test_clean_build_step_single_part(self): self.assert_files_exist() self.run_snapcraft(['clean', 'part1', '--step=build']) self.assertThat(self.parts['part1']['builddir'], Not(DirExists())) self.assertThat(self.parts['part1']['installdir'], Not(DirExists())) self.assertThat(self.parts['part1']['sourcedir'], DirExists()) self.assertThat( os.path.join(self.parts['part2']['builddir'], 'file2'), FileExists()) self.assertThat( os.path.join(self.parts['part2']['bindir'], 'file2'), FileExists()) # Now try to build again self.run_snapcraft('build') self.assert_files_exist()
def test_install_dir_sweeps_aside_dot_new_and_dot_old_if_any(self): # If directories <old>.old or <old>.new already exist, they're # probably from an aborted previous run. They won't stop # install_dir from doing its work. download_image = os.path.join(self.make_dir(), 'download-image') published_image = os.path.join(self.make_dir(), factory.getRandomString()) contents = factory.getRandomString() os.makedirs(download_image) sample_file = factory.make_file(download_image, contents=contents) os.makedirs('%s.old' % published_image) os.makedirs('%s.new' % published_image) install_dir(download_image, published_image) self.assertThat( os.path.join(published_image, os.path.basename(sample_file)), FileContains(contents)) self.assertThat('%s.old' % published_image, Not(DirExists())) self.assertThat('%s.new' % published_image, Not(DirExists()))
def test_make_destination_creates_directory_if_not_present(self): tftproot = self.make_dir() arch, subarch, release, purpose = make_arch_subarch_release_purpose() expected_destination = os.path.dirname( locate_tftp_path(compose_image_path(arch, subarch, release, purpose), tftproot=tftproot)) make_destination(tftproot, arch, subarch, release, purpose) self.assertThat(expected_destination, DirExists())
def test_clean_stage_step(self): self.assert_files_exist() output = self.run_snapcraft( ['clean', '--step=stage'], debug=False) self.assertThat(self.stage_dir, Not(DirExists())) self.assertThat(self.parts_dir, DirExists()) # Assert that the priming and staging areas were removed wholesale, not # a part at a time (since we didn't specify any parts). self.assertThat(output, Contains("Cleaning up priming area")) self.assertThat(output, Contains("Cleaning up staging area")) self.expectThat(output, Not(Contains('part1'))) self.expectThat(output, Not(Contains('part2'))) # Now try to stage again self.run_snapcraft('stage') self.assert_files_exist()
def test_out_of_order(self): # If a file or a subdirectory is listed before its parent directory, # that doesn't matter. We'll create the directory first. fixture = FileTree(['a/b/', 'a/']) with fixture: path = fixture.path self.assertThat(path, DirContains(['a'])) self.assertThat(os.path.join(path, 'a'), DirContains(['b'])) self.assertThat(os.path.join(path, 'a', 'b'), DirExists())
def test_cleaning_with_strip_does_prime_and_warns(self): self.make_snapcraft_yaml(n=3) result = self.run_command(['clean', '--step=strip']) self.assertThat(result.exit_code, Equals(0)) self.assertThat(result.output, Contains( 'DEPRECATED: Use `prime` instead of `strip` as the step to clean')) self.assertThat(self.prime_dir, Not(DirExists()))
def test_pull_ignores_snapcraft_specific_data(self): # Make the snapcraft-specific directories os.makedirs(os.path.join("src", "parts")) os.makedirs(os.path.join("src", "stage")) os.makedirs(os.path.join("src", "prime")) os.makedirs(os.path.join("src", ".snapcraft")) os.makedirs(os.path.join("src", "snap")) # Make the snapcraft.yaml (and hidden one) and a built snap open(os.path.join("src", "snapcraft.yaml"), "w").close() open(os.path.join("src", ".snapcraft.yaml"), "w").close() open(os.path.join("src", "foo.snap"), "w").close() # Make the global state cache open(os.path.join("src", ".snapcraft", "state"), "w").close() # Now make some real files os.makedirs(os.path.join("src", "dir")) open(os.path.join("src", "dir", "file"), "w").close() os.mkdir("destination") local = sources.Local("src", "destination") local.pull() # Verify that the snapcraft-specific stuff got filtered out self.assertThat(os.path.join("destination", "parts"), Not(DirExists())) self.assertThat(os.path.join("destination", "stage"), Not(DirExists())) self.assertThat(os.path.join("destination", "prime"), Not(DirExists())) self.assertThat(os.path.join("destination", "snap"), DirExists()) self.assertThat(os.path.join("destination", ".snapcraft.yaml"), FileExists()) self.assertThat(os.path.join("destination", "snapcraft.yaml"), FileExists()) self.assertThat(os.path.join("destination", "foo.snap"), Not(FileExists())) # Verify that the real stuff made it in. self.assertFalse(os.path.islink("destination")) self.assertFalse(os.path.islink(os.path.join("destination", "dir"))) self.assertGreater( os.stat(os.path.join("destination", "dir", "file")).st_nlink, 1)
def test_clean_removes_global_state(self): self.make_snapcraft_yaml( textwrap.dedent("""\ parts: test-part: plugin: nil """)) lifecycle.execute('pull', self.project_options) lifecycle.clean(self.project_options, parts=None) self.assertThat(os.path.join('snap', '.snapcraft'), Not(DirExists()))
def test_prime_excludes_internal_snapcraft_dir(self): self.make_snapcraft_yaml( textwrap.dedent("""\ parts: test-part: plugin: nil """)) lifecycle.execute('prime', self.project_options) self.assertThat(os.path.join('prime', 'snap', '.snapcraft'), Not(DirExists()))
def test_prime_excludes_internal_snapcraft_dir(self): project_config = self.make_snapcraft_project( textwrap.dedent("""\ parts: test-part: plugin: nil """)) lifecycle.execute(steps.PRIME, project_config) self.assertThat(os.path.join(steps.PRIME.name, "snap", ".snapcraft"), Not(DirExists()))
def test_clean_removes_global_state(self): project_config = self.make_snapcraft_project( textwrap.dedent("""\ parts: test-part: plugin: nil """)) lifecycle.execute(steps.PULL, project_config) lifecycle.clean(project_config.project, parts=None) self.assertThat(os.path.join("snap", ".snapcraft"), Not(DirExists()))
def test_pull_with_pip_requirements_list(self): project_dir = 'pip-requirements-list' self.run_snapcraft('pull', project_dir) self.assertThat( os.path.join(project_dir, 'parts', 'python2', 'install', 'usr', 'lib', 'python2.7', 'argparse.py'), FileExists()) self.assertThat( os.path.join(project_dir, 'parts', 'python2', 'install', 'usr', 'lib', 'python2.7', 'dist-packages', 'jsonschema'), DirExists()) self.assertThat( glob.glob( os.path.join(project_dir, 'parts', 'python3', 'install', 'usr', 'lib', 'python3*', 'argparse.py'))[0], FileExists()) self.assertThat( os.path.join(project_dir, 'parts', 'python3', 'install', 'usr', 'lib', 'python3', 'dist-packages', 'jsonschema'), DirExists())
def test_clean_stage_step_single_part(self): self.assert_files_exist() self.run_snapcraft(['clean', 'part1', '--step=stage']) self.assertThat(os.path.join(self.bindir, 'file1'), Not(FileExists())) self.assertThat(os.path.join(self.bindir, 'file2'), FileExists()) self.assertThat(self.parts_dir, DirExists()) # Now try to stage again self.run_snapcraft('stage') self.assert_files_exist()
def test_clean_pull(self): class Options: pass plugin = conda.CondaPlugin("test-part", Options(), self.project) os.makedirs(plugin._conda_home) plugin.clean_pull() self.assertThat(plugin._conda_home, Not(DirExists()))
def test_launch_instance(self): provider = ProviderImpl(project=self.project, echoer=self.echoer_mock) provider.start_mock.side_effect = errors.ProviderInstanceNotFoundError( instance_name=self.instance_name) provider.launch_instance() provider.launch_mock.assert_any_call() provider.start_mock.assert_any_call() provider.run_mock.assert_called_once_with(["snapcraft", "refresh"]) self.assertThat(provider.provider_project_dir, DirExists())
def test_clean_prime_step(self): bindir = os.path.join(self.prime_dir, "bin") self.assertThat(os.path.join(bindir, "file1"), FileExists()) self.assertThat(os.path.join(bindir, "file2"), FileExists()) output = self.run_snapcraft(["clean", "--step=prime"], debug=False) self.assertThat(self.prime_dir, Not(DirExists())) self.assertThat(self.stage_dir, DirExists()) self.assertThat(self.parts_dir, DirExists()) # Assert that the priming area was removed wholesale, not a part at a # time (since we didn't specify any parts). self.assertThat(output, Contains("Cleaning up priming area")) self.expectThat(output, Not(Contains("part1"))) self.expectThat(output, Not(Contains("part2"))) # Now try to prime again self.run_snapcraft("prime") self.assertThat(os.path.join(bindir, "file1"), FileExists()) self.assertThat(os.path.join(bindir, "file2"), FileExists())
def test_build_from_local_fixes_symlinks(self): plugin = nodejs.NodePlugin("test-part", self.options, self.project_options) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, "package.json"), "w").close() # Setup stub project symlinks modules_path = os.path.join("lib", "node_modules") project_path = os.path.join(plugin.installdir, modules_path, "project") dep_path = os.path.join(modules_path, "dep") os.makedirs(os.path.join(plugin.builddir, dep_path)) os.makedirs(os.path.join(plugin.installdir, modules_path)) os.symlink(os.path.join("..", "..", "..", "build"), project_path) plugin.build() self.assertThat(project_path, DirExists()) self.assertThat(os.path.join(project_path, dep_path), DirExists()) self.assertFalse(os.path.islink(project_path))
def test_snapped_library_takes_precedence_over_system(self): self.run_snapcraft("stage", "fake-curl-library") self.run_snapcraft(["prime", "main", "fake-curl"]) # We will have everything in, given that we require dependencies # to be primed. self.assertThat(os.path.join(self.prime_dir, "bin", "main"), FileExists()) self.assertThat(os.path.join(self.prime_dir, "lib"), DirExists()) # If this exist, snapcraft brought libbcurl in from the host. self.assertThat(os.path.join(self.prime_dir, "usr"), Not(DirExists())) # Prime the rest of the way. self.run_snapcraft("prime") # Now verify the lib we got was the one from the snap, not from the # system. self.assertThat(os.path.join(self.prime_dir, "lib", "libcurl.so"), FileExists()) self.assertThat(os.path.join(self.prime_dir, "usr"), Not(DirExists()))
def test_build_from_local_fixes_symlinks(self): plugin = nodejs.NodePlugin('test-part', self.options, self.project_options) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, 'package.json'), 'w').close() # Setup stub project symlinks modules_path = os.path.join('lib', 'node_modules') project_path = os.path.join(plugin.installdir, modules_path, 'project') dep_path = os.path.join(modules_path, 'dep') os.makedirs(os.path.join(plugin.builddir, dep_path)) os.makedirs(os.path.join(plugin.installdir, modules_path)) os.symlink(os.path.join('..', '..', '..', 'build'), project_path) plugin.build() self.assertThat(project_path, DirExists()) self.assertThat(os.path.join(project_path, dep_path), DirExists()) self.assertFalse(os.path.islink(project_path))
def test_mktemp_doesnt_exist(self, test): """ ``mktemp`` returns a path that doesn't exist inside a directory that does. """ temp_path = FilePath(test.mktemp()) self.addCleanup(_remove_dir, temp_path.parent()) self.expectThat(temp_path.parent().path, DirExists()) self.expectThat(temp_path.path, Not(PathExists())) self.assertThat(temp_path, BelowPath(FilePath(os.getcwd())))
def test_clean_stage_step_single_part(self): self.assert_files_exist() self.run_snapcraft(["clean", "part1", "--step=stage"]) self.assertThat(os.path.join(self.bindir, "file1"), Not(FileExists())) self.assertThat(os.path.join(self.bindir, "file2"), FileExists()) self.assertThat(self.parts_dir, DirExists()) # Now try to stage again self.run_snapcraft("stage") self.assert_files_exist()
def test_clean_pull_step_single_part(self): self.assert_files_exist() self.run_snapcraft(["clean", "part1", "--step=pull"]) self.assertThat(self.part1_sourcedir, Not(DirExists())) self.assertThat(os.path.join(self.part2_sourcedir, "file2"), FileExists()) # Now try to pull again self.run_snapcraft("pull") self.assert_files_exist()
def test_cleans_up_on_exception_exit(self): class DeliberateFailure(Exception): pass with ExpectedException(DeliberateFailure): with tempdir() as directory: file_path = factory.make_file(directory) raise DeliberateFailure("Exiting context by exception") self.assertThat(directory, Not(DirExists())) self.assertThat(file_path, Not(FileExists()))
def test_creates_real_fresh_directory(self): stored_text = factory.make_string() filename = factory.make_name("test-file") with tempdir() as directory: self.assertThat(directory, DirExists()) write_text_file(os.path.join(directory, filename), stored_text) retrieved_text = read_text_file(os.path.join(directory, filename)) files = os.listdir(directory) self.assertEqual(stored_text, retrieved_text) self.assertEqual([filename], files)
def test_snapped_library_takes_precedence_over_system(self): self.run_snapcraft('stage', 'fake-curl-library') self.run_snapcraft(['prime', 'main', 'fake-curl']) # We will have everything in, given that we require dependencies # to be primed. self.assertThat(os.path.join(self.prime_dir, 'bin', 'main'), FileExists()) self.assertThat(os.path.join(self.prime_dir, 'lib'), DirExists()) # If this exist, snapcraft brought libbcurl in from the host. self.assertThat(os.path.join(self.prime_dir, 'usr'), Not(DirExists())) # Prime the rest of the way. self.run_snapcraft('prime') # Now verify the lib we got was the one from the snap, not from the # system. self.assertThat(os.path.join(self.prime_dir, 'lib', 'libcurl.so'), FileExists()) self.assertThat(os.path.join(self.prime_dir, 'usr'), Not(DirExists()))
def test_clean_pull_step_single_part(self): self.assert_files_exist() self.run_snapcraft(['clean', 'part1', '--step=pull']) self.assertThat(self.part1_sourcedir, Not(DirExists())) self.assertThat(os.path.join(self.part2_sourcedir, 'file2'), FileExists()) # Now try to pull again self.run_snapcraft('pull') self.assert_files_exist()