| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import sys |
| 10 |
|
| 11 |
import mozunit |
| 12 |
import pytest |
| 13 |
from moztest.selftest.fixtures import binary_fixture # noqa: F401 |
| 14 |
from mozversion import errors, get_version |
| 15 |
|
| 16 |
"""test getting application version information from a binary path""" |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture() |
| 20 |
def fake_binary(tmpdir): |
| 21 |
binary = tmpdir.join("binary") |
| 22 |
binary.write("foobar") |
| 23 |
return str(binary) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(name="application_ini") |
| 27 |
def fixture_application_ini(tmpdir): |
| 28 |
ini = tmpdir.join("application.ini") |
| 29 |
ini.write( |
| 30 |
"""[App] |
| 31 |
ID = AppID |
| 32 |
Name = AppName |
| 33 |
CodeName = AppCodeName |
| 34 |
Version = AppVersion |
| 35 |
BuildID = AppBuildID |
| 36 |
SourceRepository = AppSourceRepo |
| 37 |
SourceStamp = AppSourceStamp |
| 38 |
Vendor = AppVendor""" |
| 39 |
) |
| 40 |
return str(ini) |
| 41 |
|
| 42 |
|
| 43 |
@pytest.fixture(name="platform_ini") |
| 44 |
def fixture_platform_ini(tmpdir): |
| 45 |
ini = tmpdir.join("platform.ini") |
| 46 |
ini.write( |
| 47 |
"""[Build] |
| 48 |
BuildID = PlatformBuildID |
| 49 |
Milestone = PlatformMilestone |
| 50 |
SourceStamp = PlatformSourceStamp |
| 51 |
SourceRepository = PlatformSourceRepo""" |
| 52 |
) |
| 53 |
return str(ini) |
| 54 |
|
| 55 |
|
| 56 |
def test_real_binary(binary): # noqa: F811 |
| 57 |
if not binary: |
| 58 |
pytest.skip("No binary found") |
| 59 |
v = get_version(binary) |
| 60 |
assert isinstance(v, dict) |
| 61 |
|
| 62 |
|
| 63 |
def test_binary(fake_binary, application_ini, platform_ini): |
| 64 |
_check_version(get_version(fake_binary)) |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.skipif( |
| 68 |
not hasattr(os, "symlink"), reason="os.symlink not supported on this platform" |
| 69 |
) |
| 70 |
def test_symlinked_binary(fake_binary, application_ini, platform_ini, tmpdir): |
| 71 |
# create a symlink of the binary in another directory and check |
| 72 |
# version against this symlink |
| 73 |
symlink = str(tmpdir.join("symlink")) |
| 74 |
os.symlink(fake_binary, symlink) |
| 75 |
_check_version(get_version(symlink)) |
| 76 |
|
| 77 |
|
| 78 |
def test_binary_in_current_path(fake_binary, application_ini, platform_ini, tmpdir): |
| 79 |
os.chdir(str(tmpdir)) |
| 80 |
_check_version(get_version()) |
| 81 |
|
| 82 |
|
| 83 |
def test_with_ini_files_on_osx( |
| 84 |
fake_binary, application_ini, platform_ini, monkeypatch, tmpdir |
| 85 |
): |
| 86 |
monkeypatch.setattr(sys, "platform", "darwin") |
| 87 |
# get_version is working with ini files next to the binary |
| 88 |
_check_version(get_version(binary=fake_binary)) |
| 89 |
|
| 90 |
# or if they are in the Resources dir |
| 91 |
# in this case the binary must be in a Contents dir, next |
| 92 |
# to the Resources dir |
| 93 |
contents_dir = tmpdir.mkdir("Contents") |
| 94 |
moved_binary = str(contents_dir.join(os.path.basename(fake_binary))) |
| 95 |
shutil.move(fake_binary, moved_binary) |
| 96 |
|
| 97 |
resources_dir = str(tmpdir.mkdir("Resources")) |
| 98 |
shutil.move(application_ini, resources_dir) |
| 99 |
shutil.move(platform_ini, resources_dir) |
| 100 |
|
| 101 |
_check_version(get_version(binary=moved_binary)) |
| 102 |
|
| 103 |
|
| 104 |
def test_invalid_binary_path(tmpdir): |
| 105 |
with pytest.raises(IOError): |
| 106 |
get_version(str(tmpdir.join("invalid"))) |
| 107 |
|
| 108 |
|
| 109 |
def test_without_ini_files(fake_binary): |
| 110 |
"""With missing ini files an exception should be thrown""" |
| 111 |
with pytest.raises(errors.AppNotFoundError): |
| 112 |
get_version(fake_binary) |
| 113 |
|
| 114 |
|
| 115 |
def test_without_platform_ini_file(fake_binary, application_ini): |
| 116 |
"""With a missing platform.ini file an exception should be thrown""" |
| 117 |
with pytest.raises(errors.AppNotFoundError): |
| 118 |
get_version(fake_binary) |
| 119 |
|
| 120 |
|
| 121 |
def test_without_application_ini_file(fake_binary, platform_ini): |
| 122 |
"""With a missing application.ini file an exception should be thrown""" |
| 123 |
with pytest.raises(errors.AppNotFoundError): |
| 124 |
get_version(fake_binary) |
| 125 |
|
| 126 |
|
| 127 |
def test_with_exe(application_ini, platform_ini, tmpdir): |
| 128 |
"""Test that we can resolve .exe files""" |
| 129 |
binary = tmpdir.join("binary.exe") |
| 130 |
binary.write("foobar") |
| 131 |
_check_version(get_version(os.path.splitext(str(binary))[0])) |
| 132 |
|
| 133 |
|
| 134 |
def test_not_found_with_binary_specified(fake_binary): |
| 135 |
with pytest.raises(errors.LocalAppNotFoundError): |
| 136 |
get_version(fake_binary) |
| 137 |
|
| 138 |
|
| 139 |
def _check_version(version): |
| 140 |
assert version.get("application_id") == "AppID" |
| 141 |
assert version.get("application_name") == "AppName" |
| 142 |
assert version.get("application_display_name") == "AppCodeName" |
| 143 |
assert version.get("application_version") == "AppVersion" |
| 144 |
assert version.get("application_buildid") == "AppBuildID" |
| 145 |
assert version.get("application_repository") == "AppSourceRepo" |
| 146 |
assert version.get("application_changeset") == "AppSourceStamp" |
| 147 |
assert version.get("application_vendor") == "AppVendor" |
| 148 |
assert version.get("platform_name") is None |
| 149 |
assert version.get("platform_buildid") == "PlatformBuildID" |
| 150 |
assert version.get("platform_repository") == "PlatformSourceRepo" |
| 151 |
assert version.get("platform_changeset") == "PlatformSourceStamp" |
| 152 |
assert version.get("invalid_key") is None |
| 153 |
assert version.get("platform_version") == "PlatformMilestone" |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
mozunit.main() |