def test_both_errors_and_warnings(emitter, config, monkeypatch, tmp_path): """Show error and warnings results.""" # fake results from the analyzer linting_results = [ linters.CheckResult( name="check-lint-1", check_type=linters.CheckType.lint, url="url-1", text="text-1", result=linters.ERRORS, ), linters.CheckResult( name="check-lint-2", check_type=linters.CheckType.lint, url="url-2", text="text-2", result=linters.WARNINGS, ), ] fake_charm = create_a_valid_zip(tmp_path) args = Namespace(filepath=fake_charm, force=None, format=None) monkeypatch.setattr(linters, "analyze", lambda *a, **k: linting_results) retcode = AnalyzeCommand(config).run(args) expected = [ "Lint Warnings:", "- check-lint-2: text-2 (url-2)", "Lint Errors:", "- check-lint-1: text-1 (url-1)", ] emitter.assert_messages(expected) assert retcode == 2
def test_both_errors_and_warnings(caplog, config, monkeypatch, tmp_path): """Show error and warnings results.""" caplog.set_level(logging.DEBUG, logger="charmcraft") # fake results from the analyzer linting_results = [ linters.CheckResult( name="check-lint-1", check_type=linters.CheckType.lint, url="url-1", text="text-1", result=linters.ERRORS, ), linters.CheckResult( name="check-lint-2", check_type=linters.CheckType.lint, url="url-2", text="text-2", result=linters.WARNINGS, ), ] fake_charm = create_a_valid_zip(tmp_path) args = Namespace(filepath=fake_charm, force=None, format=None) monkeypatch.setattr(linters, "analyze", lambda *a, **k: linting_results) retcode = AnalyzeCommand("group", config).run(args) expected = [ "Lint Warnings:", "- check-lint-2: text-2 (url-2)", "Lint Errors:", "- check-lint-1: text-1 (url-1)", ] assert expected == [rec.message for rec in caplog.records] assert retcode == 2
def test_only_fatal(caplog, config, monkeypatch, tmp_path): """Show only fatal lint results (the rest may be ignored).""" caplog.set_level(logging.DEBUG, logger="charmcraft") # fake results from the analyzer linting_results = [ linters.CheckResult( name="check-lint", check_type=linters.CheckType.lint, url="url", text="text", result=linters.FATAL, ), ] fake_charm = create_a_valid_zip(tmp_path) args = Namespace(filepath=fake_charm, force=None, format=None) monkeypatch.setattr(linters, "analyze", lambda *a, **k: linting_results) retcode = AnalyzeCommand("group", config).run(args) expected = [ "Lint Fatal:", "- check-lint (url)", ] assert expected == [rec.message for rec in caplog.records] assert retcode == 1
def test_manifest_checkers_multiple(tmp_path): """Multiple checkers, attributes and a linter.""" linting_results = [ linters.CheckResult( name="attrib-name-1", check_type=linters.CheckType.attribute, url="url", text="text", result="result-1", ), linters.CheckResult( name="attrib-name-2", check_type=linters.CheckType.attribute, url="url", text="text", result="result-2", ), linters.CheckResult( name="warning-name", check_type=linters.CheckType.lint, url="url", text="text", result="result", ), ] tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system="SuperUbuntu", release="40.10", machine="SomeRISC") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp, None, linting_results) assert result_filepath == tmp_path / "manifest.yaml" saved = yaml.safe_load(result_filepath.read_text()) expected = [ { "name": "attrib-name-1", "result": "result-1", }, { "name": "attrib-name-2", "result": "result-2", }, ] assert saved["analysis"]["attributes"] == expected
def test_only_fatal(emitter, config, monkeypatch, tmp_path): """Show only fatal lint results (the rest may be ignored).""" # fake results from the analyzer linting_results = [ linters.CheckResult( name="check-lint", check_type=linters.CheckType.lint, url="url", text="text", result=linters.FATAL, ), ] fake_charm = create_a_valid_zip(tmp_path) args = Namespace(filepath=fake_charm, force=None, format=None) monkeypatch.setattr(linters, "analyze", lambda *a, **k: linting_results) retcode = AnalyzeCommand(config).run(args) expected = [ "Lint Fatal:", "- check-lint (url)", ] emitter.assert_messages(expected) assert retcode == 1
def test_complete_set_of_results(emitter, config, monkeypatch, tmp_path, indicated_format): """Show a complete basic case of results.""" # fake results from the analyzer linting_results = [ linters.CheckResult( name="check-lint-01", check_type=linters.CheckType.lint, url="url-01", text="text-01", result=linters.WARNINGS, ), linters.CheckResult( name="check-lint-02", check_type=linters.CheckType.lint, url="url-02", text="text-02", result=linters.OK, ), linters.CheckResult( name="check-lint-03", check_type=linters.CheckType.lint, url="url-03", text="text-03", result=linters.ERRORS, ), linters.CheckResult( name="check-attribute-04", check_type=linters.CheckType.attribute, url="url-04", text="text-04", result="check-result-04", ), linters.CheckResult( name="check-attribute-05", check_type=linters.CheckType.attribute, url="url-05", text="text-05", result=linters.IGNORED, ), linters.CheckResult( name="check-lint-06", check_type=linters.CheckType.lint, url="url-06", text="text-06", result=linters.IGNORED, ), linters.CheckResult( name="check-lint-07", check_type=linters.CheckType.lint, url="url-07", text="text-07", result=linters.FATAL, ), ] fake_charm = create_a_valid_zip(tmp_path) args = Namespace(filepath=fake_charm, force=None, format=indicated_format) monkeypatch.setattr(linters, "analyze", lambda *a, **k: linting_results) with patch.object(linters, "analyze") as mock_analyze: mock_analyze.return_value = linting_results AnalyzeCommand(config).run(args) mock_analyze.assert_called_with(config, ANY, override_ignore_config=False) if indicated_format is None: expected = [ "Attributes:", "- check-attribute-04: check-result-04 (url-04)", "- check-attribute-05: ignored (url-05)", "Lint Ignored:", "- check-lint-06 (url-06)", "Lint Warnings:", "- check-lint-01: text-01 (url-01)", "Lint Errors:", "- check-lint-03: text-03 (url-03)", "Lint Fatal:", "- check-lint-07 (url-07)", "Lint OK:", "- check-lint-02: no issues found (url-02)", ] emitter.assert_messages(expected) else: expected = [ { "name": "check-lint-01", "type": "lint", "url": "url-01", "result": "warnings", }, { "name": "check-lint-02", "type": "lint", "url": "url-02", "result": "ok", }, { "name": "check-lint-03", "type": "lint", "url": "url-03", "result": "errors", }, { "name": "check-attribute-04", "type": "attribute", "url": "url-04", "result": "check-result-04", }, { "name": "check-attribute-05", "type": "attribute", "url": "url-05", "result": "ignored", }, { "name": "check-lint-06", "type": "lint", "url": "url-06", "result": "ignored", }, { "name": "check-lint-07", "type": "lint", "url": "url-07", "result": "fatal", }, ] text = emitter.assert_message(r"\[.*\]", regex=True) assert expected == json.loads(text)
def test_manifest_simple_ok(tmp_path): """Simple construct.""" bases_config = config.BasesConfiguration( **{ "build-on": [ config.Base( name="test-name", channel="test-channel", ), ], "run-on": [ config.Base( name="test-name", channel="test-channel", architectures=["arch1"], ), config.Base( name="test-name2", channel="test-channel2", architectures=["arch1", "arch2"], ), ], } ) linting_results = [ linters.CheckResult( name="check-name", check_type=linters.CheckType.attribute, url="url", text="text", result="check-result", ), ] tstamp = datetime.datetime(2020, 2, 1, 15, 40, 33) os_platform = OSPlatform(system="SuperUbuntu", release="40.10", machine="SomeRISC") with patch("charmcraft.utils.get_os_platform", return_value=os_platform): result_filepath = create_manifest(tmp_path, tstamp, bases_config, linting_results) assert result_filepath == tmp_path / "manifest.yaml" saved = yaml.safe_load(result_filepath.read_text()) expected = { "charmcraft-started-at": "2020-02-01T15:40:33Z", "charmcraft-version": __version__, "bases": [ { "name": "test-name", "channel": "test-channel", "architectures": ["arch1"], }, { "name": "test-name2", "channel": "test-channel2", "architectures": ["arch1", "arch2"], }, ], "analysis": { "attributes": [ { "name": "check-name", "result": "check-result", }, ], }, } assert saved == expected