summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozversion/tests/test_binary.py
blob: 9de6bb0e6b7e125aec2d2a494c1006cb5b971f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import shutil
import sys

import mozunit
import pytest
from moztest.selftest.fixtures import binary_fixture  # noqa: F401
from mozversion import errors, get_version

"""test getting application version information from a binary path"""


@pytest.fixture()
def fake_binary(tmpdir):
    binary = tmpdir.join("binary")
    binary.write("foobar")
    return str(binary)


@pytest.fixture(name="application_ini")
def fixture_application_ini(tmpdir):
    ini = tmpdir.join("application.ini")
    ini.write(
        """[App]
ID = AppID
Name = AppName
CodeName = AppCodeName
Version = AppVersion
BuildID = AppBuildID
SourceRepository = AppSourceRepo
SourceStamp = AppSourceStamp
Vendor = AppVendor"""
    )
    return str(ini)


@pytest.fixture(name="platform_ini")
def fixture_platform_ini(tmpdir):
    ini = tmpdir.join("platform.ini")
    ini.write(
        """[Build]
BuildID = PlatformBuildID
Milestone = PlatformMilestone
SourceStamp = PlatformSourceStamp
SourceRepository = PlatformSourceRepo"""
    )
    return str(ini)


def test_real_binary(binary):  # noqa: F811
    if not binary:
        pytest.skip("No binary found")
    v = get_version(binary)
    assert isinstance(v, dict)


def test_binary(fake_binary, application_ini, platform_ini):
    _check_version(get_version(fake_binary))


@pytest.mark.skipif(
    not hasattr(os, "symlink"), reason="os.symlink not supported on this platform"
)
def test_symlinked_binary(fake_binary, application_ini, platform_ini, tmpdir):
    # create a symlink of the binary in another directory and check
    # version against this symlink
    symlink = str(tmpdir.join("symlink"))
    os.symlink(fake_binary, symlink)
    _check_version(get_version(symlink))


def test_binary_in_current_path(fake_binary, application_ini, platform_ini, tmpdir):
    os.chdir(str(tmpdir))
    _check_version(get_version())


def test_with_ini_files_on_osx(
    fake_binary, application_ini, platform_ini, monkeypatch, tmpdir
):
    monkeypatch.setattr(sys, "platform", "darwin")
    # get_version is working with ini files next to the binary
    _check_version(get_version(binary=fake_binary))

    # or if they are in the Resources dir
    # in this case the binary must be in a Contents dir, next
    # to the Resources dir
    contents_dir = tmpdir.mkdir("Contents")
    moved_binary = str(contents_dir.join(os.path.basename(fake_binary)))
    shutil.move(fake_binary, moved_binary)

    resources_dir = str(tmpdir.mkdir("Resources"))
    shutil.move(application_ini, resources_dir)
    shutil.move(platform_ini, resources_dir)

    _check_version(get_version(binary=moved_binary))


def test_invalid_binary_path(tmpdir):
    with pytest.raises(IOError):
        get_version(str(tmpdir.join("invalid")))


def test_without_ini_files(fake_binary):
    """With missing ini files an exception should be thrown"""
    with pytest.raises(errors.AppNotFoundError):
        get_version(fake_binary)


def test_without_platform_ini_file(fake_binary, application_ini):
    """With a missing platform.ini file an exception should be thrown"""
    with pytest.raises(errors.AppNotFoundError):
        get_version(fake_binary)


def test_without_application_ini_file(fake_binary, platform_ini):
    """With a missing application.ini file an exception should be thrown"""
    with pytest.raises(errors.AppNotFoundError):
        get_version(fake_binary)


def test_with_exe(application_ini, platform_ini, tmpdir):
    """Test that we can resolve .exe files"""
    binary = tmpdir.join("binary.exe")
    binary.write("foobar")
    _check_version(get_version(os.path.splitext(str(binary))[0]))


def test_not_found_with_binary_specified(fake_binary):
    with pytest.raises(errors.LocalAppNotFoundError):
        get_version(fake_binary)


def _check_version(version):
    assert version.get("application_id") == "AppID"
    assert version.get("application_name") == "AppName"
    assert version.get("application_display_name") == "AppCodeName"
    assert version.get("application_version") == "AppVersion"
    assert version.get("application_buildid") == "AppBuildID"
    assert version.get("application_repository") == "AppSourceRepo"
    assert version.get("application_changeset") == "AppSourceStamp"
    assert version.get("application_vendor") == "AppVendor"
    assert version.get("platform_name") is None
    assert version.get("platform_buildid") == "PlatformBuildID"
    assert version.get("platform_repository") == "PlatformSourceRepo"
    assert version.get("platform_changeset") == "PlatformSourceStamp"
    assert version.get("invalid_key") is None
    assert version.get("platform_version") == "PlatformMilestone"


if __name__ == "__main__":
    mozunit.main()