|
| 1 | +# Copyright (C) 2025 The Android Open Source Project |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unittests for the subcmds/manifest.py module.""" |
| 16 | + |
| 17 | +import json |
| 18 | +from pathlib import Path |
| 19 | +from unittest import mock |
| 20 | + |
| 21 | +import manifest_xml |
| 22 | +from subcmds import manifest |
| 23 | + |
| 24 | + |
| 25 | +_EXAMPLE_MANIFEST = """\ |
| 26 | +<?xml version="1.0" encoding="UTF-8"?> |
| 27 | +<manifest> |
| 28 | + <remote name="test-remote" fetch="http://localhost" /> |
| 29 | + <default remote="test-remote" revision="refs/heads/main" /> |
| 30 | + <project name="repohooks" path="src/repohooks"/> |
| 31 | + <repo-hooks in-project="repohooks" enabled-list="a, b"/> |
| 32 | +</manifest> |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +def _get_cmd(repodir: Path) -> manifest.Manifest: |
| 37 | + """Instantiate a manifest command object to test.""" |
| 38 | + manifests_git = repodir / "manifests.git" |
| 39 | + manifests_git.mkdir() |
| 40 | + (manifests_git / "config").write_text( |
| 41 | + """ |
| 42 | +[remote "origin"] |
| 43 | +\turl = http://localhost/manifest |
| 44 | +""" |
| 45 | + ) |
| 46 | + client = manifest_xml.RepoClient(repodir=str(repodir)) |
| 47 | + git_event_log = mock.MagicMock(ErrorEvent=mock.Mock(return_value=None)) |
| 48 | + return manifest.Manifest( |
| 49 | + repodir=client.repodir, |
| 50 | + client=client, |
| 51 | + manifest=client.manifest, |
| 52 | + outer_client=client, |
| 53 | + outer_manifest=client.manifest, |
| 54 | + git_event_log=git_event_log, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def test_output_format_xml_file(tmp_path): |
| 59 | + """Test writing XML to a file.""" |
| 60 | + path = tmp_path / "manifest.xml" |
| 61 | + path.write_text(_EXAMPLE_MANIFEST) |
| 62 | + outpath = tmp_path / "output.xml" |
| 63 | + cmd = _get_cmd(tmp_path) |
| 64 | + opt, args = cmd.OptionParser.parse_args(["--output-file", str(outpath)]) |
| 65 | + cmd.Execute(opt, args) |
| 66 | + # Normalize the output a bit as we don't exactly care. |
| 67 | + normalize = lambda data: "\n".join( |
| 68 | + x.strip() for x in data.splitlines() if x.strip() |
| 69 | + ) |
| 70 | + assert ( |
| 71 | + normalize(outpath.read_text()) |
| 72 | + == """<?xml version="1.0" encoding="UTF-8"?> |
| 73 | +<manifest> |
| 74 | +<remote name="test-remote" fetch="http://localhost"/> |
| 75 | +<default remote="test-remote" revision="refs/heads/main"/> |
| 76 | +<project name="repohooks" path="src/repohooks"/> |
| 77 | +<repo-hooks in-project="repohooks" enabled-list="a b"/> |
| 78 | +</manifest>""" |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_output_format_xml_stdout(tmp_path, capsys): |
| 83 | + """Test writing XML to stdout.""" |
| 84 | + path = tmp_path / "manifest.xml" |
| 85 | + path.write_text(_EXAMPLE_MANIFEST) |
| 86 | + cmd = _get_cmd(tmp_path) |
| 87 | + opt, args = cmd.OptionParser.parse_args(["--format", "xml"]) |
| 88 | + cmd.Execute(opt, args) |
| 89 | + # Normalize the output a bit as we don't exactly care. |
| 90 | + normalize = lambda data: "\n".join( |
| 91 | + x.strip() for x in data.splitlines() if x.strip() |
| 92 | + ) |
| 93 | + stdout = capsys.readouterr().out |
| 94 | + assert ( |
| 95 | + normalize(stdout) |
| 96 | + == """<?xml version="1.0" encoding="UTF-8"?> |
| 97 | +<manifest> |
| 98 | +<remote name="test-remote" fetch="http://localhost"/> |
| 99 | +<default remote="test-remote" revision="refs/heads/main"/> |
| 100 | +<project name="repohooks" path="src/repohooks"/> |
| 101 | +<repo-hooks in-project="repohooks" enabled-list="a b"/> |
| 102 | +</manifest>""" |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def test_output_format_json(tmp_path, capsys): |
| 107 | + """Test writing JSON.""" |
| 108 | + path = tmp_path / "manifest.xml" |
| 109 | + path.write_text(_EXAMPLE_MANIFEST) |
| 110 | + cmd = _get_cmd(tmp_path) |
| 111 | + opt, args = cmd.OptionParser.parse_args(["--format", "json"]) |
| 112 | + cmd.Execute(opt, args) |
| 113 | + obj = json.loads(capsys.readouterr().out) |
| 114 | + assert obj == { |
| 115 | + "default": {"remote": "test-remote", "revision": "refs/heads/main"}, |
| 116 | + "project": [{"name": "repohooks", "path": "src/repohooks"}], |
| 117 | + "remote": [{"fetch": "http://localhost", "name": "test-remote"}], |
| 118 | + "repo-hooks": {"enabled-list": "a b", "in-project": "repohooks"}, |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +def test_output_format_json_pretty(tmp_path, capsys): |
| 123 | + """Test writing pretty JSON.""" |
| 124 | + path = tmp_path / "manifest.xml" |
| 125 | + path.write_text(_EXAMPLE_MANIFEST) |
| 126 | + cmd = _get_cmd(tmp_path) |
| 127 | + opt, args = cmd.OptionParser.parse_args(["--format", "json", "--pretty"]) |
| 128 | + cmd.Execute(opt, args) |
| 129 | + stdout = capsys.readouterr().out |
| 130 | + assert ( |
| 131 | + stdout |
| 132 | + == """\ |
| 133 | +{ |
| 134 | + "default": { |
| 135 | + "remote": "test-remote", |
| 136 | + "revision": "refs/heads/main" |
| 137 | + }, |
| 138 | + "project": [ |
| 139 | + { |
| 140 | + "name": "repohooks", |
| 141 | + "path": "src/repohooks" |
| 142 | + } |
| 143 | + ], |
| 144 | + "remote": [ |
| 145 | + { |
| 146 | + "fetch": "http://localhost", |
| 147 | + "name": "test-remote" |
| 148 | + } |
| 149 | + ], |
| 150 | + "repo-hooks": { |
| 151 | + "enabled-list": "a b", |
| 152 | + "in-project": "repohooks" |
| 153 | + } |
| 154 | +} |
| 155 | +""" |
| 156 | + ) |
0 commit comments