| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
from pathlib import Path |
| 6 |
from string import Template |
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
import mozunit |
| 10 |
|
| 11 |
import mozpack.pkg |
| 12 |
from mozpack.pkg import ( |
| 13 |
create_bom, |
| 14 |
create_payload, |
| 15 |
create_pkg, |
| 16 |
get_app_info_plist, |
| 17 |
get_apple_template, |
| 18 |
get_relative_glob_list, |
| 19 |
save_text_file, |
| 20 |
xar_package_folder, |
| 21 |
) |
| 22 |
from mozpack.test.test_files import TestWithTmpDir |
| 23 |
|
| 24 |
|
| 25 |
class TestPkg(TestWithTmpDir): |
| 26 |
maxDiff = None |
| 27 |
|
| 28 |
class MockSubprocessRun: |
| 29 |
stderr = "" |
| 30 |
stdout = "" |
| 31 |
returncode = 0 |
| 32 |
|
| 33 |
def __init__(self, returncode=0): |
| 34 |
self.returncode = returncode |
| 35 |
|
| 36 |
def _mk_test_file(self, name, mode=0o777): |
| 37 |
tool = Path(self.tmpdir) / f"{name}" |
| 38 |
tool.touch() |
| 39 |
tool.chmod(mode) |
| 40 |
return tool |
| 41 |
|
| 42 |
def test_get_apple_template(self): |
| 43 |
tmpl = get_apple_template("Distribution.template") |
| 44 |
assert type(tmpl) is Template |
| 45 |
|
| 46 |
def test_get_apple_template_not_file(self): |
| 47 |
with self.assertRaises(Exception): |
| 48 |
get_apple_template("tmpl-should-not-exist") |
| 49 |
|
| 50 |
def test_save_text_file(self): |
| 51 |
content = "Hello" |
| 52 |
destination = Path(self.tmpdir) / "test_save_text_file" |
| 53 |
save_text_file(content, destination) |
| 54 |
with destination.open("r") as file: |
| 55 |
assert content == file.read() |
| 56 |
|
| 57 |
def test_get_app_info_plist(self): |
| 58 |
app_path = Path(self.tmpdir) / "app" |
| 59 |
(app_path / "Contents").mkdir(parents=True) |
| 60 |
(app_path / "Contents/Info.plist").touch() |
| 61 |
data = {"foo": "bar"} |
| 62 |
with patch.object(mozpack.pkg.plistlib, "load", lambda x: data): |
| 63 |
assert data == get_app_info_plist(app_path) |
| 64 |
|
| 65 |
def test_get_app_info_plist_not_file(self): |
| 66 |
app_path = Path(self.tmpdir) / "app-does-not-exist" |
| 67 |
with self.assertRaises(Exception): |
| 68 |
get_app_info_plist(app_path) |
| 69 |
|
| 70 |
def _mock_payload(self, returncode): |
| 71 |
def _mock_run(*args, **kwargs): |
| 72 |
return self.MockSubprocessRun(returncode) |
| 73 |
|
| 74 |
return _mock_run |
| 75 |
|
| 76 |
def test_create_payload(self): |
| 77 |
destination = Path(self.tmpdir) / "mockPayload" |
| 78 |
with patch.object(mozpack.pkg.subprocess, "run", self._mock_payload(0)): |
| 79 |
create_payload(destination, Path(self.tmpdir), "cpio") |
| 80 |
|
| 81 |
def test_create_bom(self): |
| 82 |
bom_path = Path(self.tmpdir) / "Bom" |
| 83 |
bom_path.touch() |
| 84 |
root_path = Path(self.tmpdir) |
| 85 |
tool_path = Path(self.tmpdir) / "not-really-used-during-test" |
| 86 |
with patch.object(mozpack.pkg.subprocess, "check_call", lambda *x: None): |
| 87 |
create_bom(bom_path, root_path, tool_path) |
| 88 |
|
| 89 |
def get_relative_glob_list(self): |
| 90 |
source = Path(self.tmpdir) |
| 91 |
(source / "testfile").touch() |
| 92 |
glob = "*" |
| 93 |
assert len(get_relative_glob_list(source, glob)) == 1 |
| 94 |
|
| 95 |
def test_xar_package_folder(self): |
| 96 |
source = Path(self.tmpdir) |
| 97 |
dest = source / "fakedestination" |
| 98 |
dest.touch() |
| 99 |
tool = source / "faketool" |
| 100 |
with patch.object(mozpack.pkg.subprocess, "check_call", lambda *x, **y: None): |
| 101 |
xar_package_folder(source, dest, tool) |
| 102 |
|
| 103 |
def test_xar_package_folder_not_absolute(self): |
| 104 |
source = Path("./some/relative/path") |
| 105 |
dest = Path("./some/other/relative/path") |
| 106 |
tool = source / "faketool" |
| 107 |
with patch.object(mozpack.pkg.subprocess, "check_call", lambda: None): |
| 108 |
with self.assertRaises(Exception): |
| 109 |
xar_package_folder(source, dest, tool) |
| 110 |
|
| 111 |
def test_create_pkg(self): |
| 112 |
def noop(*x, **y): |
| 113 |
pass |
| 114 |
|
| 115 |
def mock_get_app_info_plist(*args): |
| 116 |
return {"CFBundleShortVersionString": "1.0.0"} |
| 117 |
|
| 118 |
def mock_get_apple_template(*args): |
| 119 |
return Template("fake template") |
| 120 |
|
| 121 |
source = Path(self.tmpdir) / "FakeApp.app" |
| 122 |
source.mkdir() |
| 123 |
output = Path(self.tmpdir) / "output.pkg" |
| 124 |
fake_tool = Path(self.tmpdir) / "faketool" |
| 125 |
with patch.multiple( |
| 126 |
mozpack.pkg, |
| 127 |
get_app_info_plist=mock_get_app_info_plist, |
| 128 |
get_apple_template=mock_get_apple_template, |
| 129 |
save_text_file=noop, |
| 130 |
create_payload=noop, |
| 131 |
create_bom=noop, |
| 132 |
xar_package_folder=noop, |
| 133 |
): |
| 134 |
create_pkg(source, output, fake_tool, fake_tool, fake_tool) |
| 135 |
|
| 136 |
|
| 137 |
if __name__ == "__main__": |
| 138 |
mozunit.main() |