def test_metadata_doesnt_overwrite_specified(self): fake_logger = fixtures.FakeLogger(level=logging.WARNING) self.useFixture(fake_logger) def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata( summary="extracted summary", description="extracted description" ) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) y = self.generate_meta_yaml(build=True) # Since both summary and description were specified, neither should be # overwritten self.assertThat(y["summary"], Equals(self.config_data["summary"])) self.assertThat(y["description"], Equals(self.config_data["description"])) # Verify that we warn that the YAML took precedence over the extracted # metadata for summary and description self.assertThat( fake_logger.output, Contains( "The 'description' and 'summary' properties are specified in " "adopted info as well as the YAML: taking the properties from the " "YAML" ), )
def test_metadata_doesnt_overwrite_desktop_file(self): os.makedirs(self.directory) desktop_content = "setup desktop" _create_file( os.path.join(self.directory, "test-app.desktop"), content=desktop_content ) def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata( desktop_file_paths=[ "usr/share/applications/com.example.test/app.desktop" ] ) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) self.generate_meta_yaml( build=True, snapcraft_yaml_file_path=os.path.join( self.snapcraft_assets_dir, "snapcraft.yaml" ), ) expected_desktop = os.path.join(self.meta_dir, "gui", "test-app.desktop") self.assertThat(expected_desktop, FileContains(desktop_content))
def test_extractor_missing_extract_function(self): fake_logger = fixtures.FakeLogger(level=logging.WARN) self.useFixture(fake_logger) open("unhandled-file", "w").close() def _fake_extractor(file_path, workdir): raise extractors.UnhandledFileError(file_path, "fake") self.useFixture( fixture_setup.FakeMetadataExtractor("fake", _fake_extractor, "not_extract")) raised = self.assertRaises( errors.UnhandledMetadataFileTypeError, extract_metadata, "test-part", "unhandled-file", workdir=".", ) self.assertThat(raised.path, Equals("unhandled-file")) self.assertThat( fake_logger.output, Contains("'fake' doesn't include the 'extract' function"), )
def test_metadata_with_unexisting_icon(self): def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata( icon="test/extracted/unexistent/icon/path" ) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) # The meta generation should just ignore the dead path, and not fail. self.generate_meta_yaml(build=True)
def test_handled_file(self): open("test-metadata-file", "w").close() def _fake_extractor(file_path): return extractors.ExtractedMetadata(summary="test summary", description="test description") self.useFixture( fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) metadata = extract_metadata("test-part", "test-metadata-file") self.assertThat(metadata.get_summary(), Equals("test summary")) self.assertThat(metadata.get_description(), Equals("test description"))
def test_handled_file(self): open('test-metadata-file', 'w').close() def _fake_extractor(file_path): return extractors.ExtractedMetadata(summary='test summary', description='test description') self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) metadata = extract_metadata('test-part', 'test-metadata-file') self.assertThat(metadata.get_summary(), Equals('test summary')) self.assertThat(metadata.get_description(), Equals('test description'))
def test_unhandled_file(self): open('unhandled-file', 'w').close() def _fake_extractor(file_path): raise extractors.UnhandledFileError(file_path, 'fake') self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) raised = self.assertRaises(errors.UnhandledMetadataFileTypeError, extract_metadata, 'test-part', 'unhandled-file') self.assertThat(raised.path, Equals('unhandled-file'))
def test_extractor_returning_invalid_things(self): open('unhandled-file', 'w').close() def _fake_extractor(file_path): return "I'm not metadata!" self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) raised = self.assertRaises(errors.InvalidExtractorValueError, extract_metadata, 'test-part', 'unhandled-file') self.assertThat(raised.path, Equals('unhandled-file')) self.assertThat(raised.extractor_name, Equals('fake'))
def test_metadata_doesnt_overwrite_specified(self): def _fake_extractor(file_path): return extractors.ExtractedMetadata( summary='extracted summary', description='extracted description') self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) y = self.generate_meta_yaml(build=True) # Since both summary and description were specified, neither should be # overwritten self.assertThat(y['summary'], Equals(self.config_data['summary'])) self.assertThat(y['description'], Equals(self.config_data['description']))
def test_scriptlets_overwrite_extracted_metadata_regardless_of_order(self): with contextlib.suppress(KeyError): del self.config_data[self.keyword] self.config_data["parts"]["test-part"][ "override-pull" ] = "snapcraftctl {} {} && snapcraftctl pull".format(self.setter, self.value) def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata(**{self.keyword: "extracted-value"}) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) generated = self.generate_meta_yaml(build=True) self.assertThat(generated[self.keyword], Equals(self.value))
def test_metadata_satisfies_required_property(self): del self.config_data["summary"] def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata( summary="extracted summary", description="extracted description" ) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) y = self.generate_meta_yaml(build=True) # Summary should come from the extracted metadata, while description # should not. self.assertThat(y["summary"], Equals("extracted summary")) self.assertThat(y["description"], Equals(self.config_data["description"]))
def test_metadata_doesnt_overwrite_icon_file(self): os.makedirs(self.directory) icon_content = 'setup icon' _create_file(os.path.join(self.directory, self.file_name), content=icon_content) def _fake_extractor(file_path): return extractors.ExtractedMetadata( icon='test/extracted/unexistent/icon/path') self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) self.generate_meta_yaml(build=True) expected_icon = os.path.join(self.meta_dir, 'gui', self.file_name) self.assertThat(expected_icon, FileContains(icon_content))
def test_metadata_not_all_properties_satisfied(self): del self.config_data["summary"] del self.config_data["description"] def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata(description="extracted description") self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) # Assert that description has been satisfied by extracted metadata, but # summary has not. raised = self.assertRaises( meta_errors.MissingSnapcraftYamlKeysError, self.generate_meta_yaml, build=True, ) self.assertThat(raised.keys, Equals("'summary'"))
def test_unhandled_file(self): open("unhandled-file", "w").close() def _fake_extractor(file_path): raise extractors.UnhandledFileError(file_path, "fake") self.useFixture( fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) raised = self.assertRaises( errors.UnhandledMetadataFileTypeError, extract_metadata, "test-part", "unhandled-file", ) self.assertThat(raised.path, Equals("unhandled-file"))
def test_metadata_satisfies_required_property(self): del self.config_data['summary'] def _fake_extractor(file_path): return extractors.ExtractedMetadata( summary='extracted summary', description='extracted description') self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) y = self.generate_meta_yaml(build=True) # Summary should come from the extracted metadata, while description # should not. self.assertThat(y['summary'], Equals('extracted summary')) self.assertThat(y['description'], Equals(self.config_data['description']))
def assert_scriptlets_overwrite_extracted_metadata( self, keyword, original, value, setter ): with contextlib.suppress(KeyError): del self.config_data[keyword] self.config_data["parts"]["test-part"][ "override-build" ] = "snapcraftctl build && snapcraftctl {} {}".format(setter, value) def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata(**{keyword: "extracted-value"}) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) generated = self.generate_meta_yaml(build=True) self.assertThat(generated[keyword], Equals(value))
def test_invalid_extracted_metadata(self): with contextlib.suppress(KeyError): del self.config_data[self.keyword] def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata(**{self.keyword: self.value}) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) raised = self.assertRaises( project_errors.YamlValidationError, self.generate_meta_yaml, build=True ) self.assertThat( str(raised), Contains( "Issues while validating properties: The {!r} property does not " "match the required schema".format(self.keyword) ), )
def test_metadata_doesnt_overwrite_desktop_file(self): os.makedirs(self.directory) desktop_content = 'setup desktop' _create_file(os.path.join(self.directory, 'test-app.desktop'), content=desktop_content) def _fake_extractor(file_path): return extractors.ExtractedMetadata(desktop_file_paths=[ 'usr/share/applications/com.example.test/app.desktop' ]) self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) self.generate_meta_yaml(build=True) expected_desktop = os.path.join(self.meta_dir, 'gui', 'test-app.desktop') self.assertThat(expected_desktop, FileContains(desktop_content))
def test_extractor_returning_invalid_things(self): open("unhandled-file", "w").close() def _fake_extractor(file_path, workdir): return "I'm not metadata!" self.useFixture( fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) raised = self.assertRaises( errors.InvalidExtractorValueError, extract_metadata, "test-part", "unhandled-file", workdir=".", ) self.assertThat(raised.path, Equals("unhandled-file")) self.assertThat(raised.extractor_name, Equals("fake"))
def test_extractor_missing_extract_function(self): fake_logger = fixtures.FakeLogger(level=logging.WARN) self.useFixture(fake_logger) open('unhandled-file', 'w').close() def _fake_extractor(file_path): raise extractors.UnhandledFileError(file_path, 'fake') self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor, 'not_extract')) raised = self.assertRaises(errors.UnhandledMetadataFileTypeError, extract_metadata, 'test-part', 'unhandled-file') self.assertThat(raised.path, Equals('unhandled-file')) self.assertThat( fake_logger.output, Contains("'fake' doesn't include the 'extract' function"))
def test_metadata_doesnt_overwrite_icon_file(self): os.makedirs(self.directory) icon_content = "setup icon" _create_file(os.path.join(self.directory, self.file_name), content=icon_content) def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata( icon="test/extracted/unexistent/icon/path" ) self.useFixture(fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) self.generate_meta_yaml( build=True, snapcraft_yaml_file_path=os.path.join( self.snapcraft_assets_dir, "snapcraft.yaml" ), ) expected_icon = os.path.join(self.meta_dir, "gui", self.file_name) self.assertThat(expected_icon, FileContains(icon_content))
def test_pull_state_with_extracted_metadata(self, repo_mock): self.handler = self.load_part( "test_part", part_properties={ "source": ".", "parse-info": ["metadata-file"] }, ) # Create metadata file open("metadata-file", "w").close() def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata( common_id="test_common_id", summary="test summary", description="test description", icon="/test/path", desktop_file_paths=[ "usr/share/applications/com.example.test/app.desktop" ], ) self.useFixture( fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) self.assertRaises(errors.NoLatestStepError, self.handler.latest_step) self.assertThat(self.handler.next_step(), Equals(steps.PULL)) repo_mock.get_installed_build_packages.return_value = [] self.handler.pull() self.assertThat(self.handler.latest_step(), Equals(steps.PULL)) self.assertThat(self.handler.next_step(), Equals(steps.BUILD)) state = self.handler.get_pull_state() self.assertTrue(state, "Expected pull to save state YAML") self.assertTrue(type(state) is states.PullState) self.assertTrue(type(state.properties) is OrderedDict) self.assertThat(len(state.properties), Equals(12)) for expected in [ "source", "source-branch", "source-commit", "source-depth", "source-subdir", "source-tag", "source-type", "source-submodules", "plugin", "stage-packages", "parse-info", "override-pull", ]: self.assertThat(state.properties, Contains(expected)) self.assertTrue(type(state.project_options) is OrderedDict) self.assertThat(state.project_options, Contains("deb_arch")) self.assertTrue(type(state.extracted_metadata) is OrderedDict) for expected in ("metadata", "files"): self.assertThat(state.extracted_metadata, Contains(expected)) metadata = state.extracted_metadata["metadata"] self.assertThat(metadata.get_common_id(), Equals("test_common_id")) self.assertThat(metadata.get_summary(), Equals("test summary")) self.assertThat(metadata.get_description(), Equals("test description")) self.assertThat(metadata.get_icon(), Equals("/test/path")) self.assertThat( metadata.get_desktop_file_paths(), Equals(["usr/share/applications/com.example.test/app.desktop"]), ) files = state.extracted_metadata["files"] self.assertThat( files, Equals( [os.path.join(self.handler.part_source_dir, "metadata-file")]))
def test_build_state_with_extracted_metadata(self): self.handler = self.load_part( "test_part", part_properties={"parse-info": ["metadata-file"]}) # Create metadata file open(os.path.join(self.handler.part_source_dir, "metadata-file"), "w").close() def _fake_extractor(file_path, workdir): return extractors.ExtractedMetadata( common_id="test_common_id", summary="test summary", description="test description", icon="/test/path", desktop_file_paths=[ "usr/share/applications/com.example.test/app.desktop" ], ) self.useFixture( fixture_setup.FakeMetadataExtractor("fake", _fake_extractor)) self.assertRaises(errors.NoLatestStepError, self.handler.latest_step) self.assertThat(self.handler.next_step(), Equals(steps.PULL)) self.handler.build() self.assertThat(self.handler.latest_step(), Equals(steps.BUILD)) self.assertThat(self.handler.next_step(), Equals(steps.STAGE)) state = self.handler.get_build_state() self.assertTrue(state, "Expected build to save state YAML") self.assertTrue(type(state) is states.BuildState) self.assertTrue(type(state.properties) is OrderedDict) self.assertThat(len(state.properties), Equals(6)) for expected in [ "after", "build-attributes", "build-packages", "disable-parallel", "organize", "override-build", ]: self.assertThat(state.properties, Contains(expected)) self.assertTrue(type(state.project_options) is OrderedDict) self.assertThat(state.project_options, Contains("deb_arch")) self.assertTrue(type(state.extracted_metadata) is OrderedDict) for expected in ("metadata", "files"): self.assertThat(state.extracted_metadata, Contains(expected)) metadata = state.extracted_metadata["metadata"] self.assertThat(metadata.get_common_id(), Equals("test_common_id")) self.assertThat(metadata.get_summary(), Equals("test summary")) self.assertThat(metadata.get_description(), Equals("test description")) self.assertThat(metadata.get_icon(), Equals("/test/path")) self.assertThat( metadata.get_desktop_file_paths(), Equals(["usr/share/applications/com.example.test/app.desktop"]), ) files = state.extracted_metadata["files"] self.assertThat( files, Equals( [os.path.join(self.handler.part_build_dir, "metadata-file")]))