|
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 mozinfo |
|
8 import mozinstall |
|
9 import mozfile |
|
10 import os |
|
11 import tarfile |
|
12 import tempfile |
|
13 import unittest |
|
14 import zipfile |
|
15 |
|
16 # Store file location at load time |
|
17 here = os.path.dirname(os.path.abspath(__file__)) |
|
18 |
|
19 class TestMozInstall(unittest.TestCase): |
|
20 |
|
21 @classmethod |
|
22 def setUpClass(cls): |
|
23 """ Setting up stub installers """ |
|
24 cls.dmg = os.path.join(here, 'Installer-Stubs', 'firefox.dmg') |
|
25 cls.exe = os.path.join(here, 'Installer-Stubs', 'firefox.exe') |
|
26 cls.zipfile = os.path.join(here, 'Installer-Stubs', 'firefox.zip') |
|
27 cls.bz2 = os.path.join(here, 'Installer-Stubs', 'firefox.tar.bz2') |
|
28 |
|
29 def setUp(self): |
|
30 self.tempdir = tempfile.mkdtemp() |
|
31 |
|
32 def tearDown(self): |
|
33 mozfile.rmtree(self.tempdir) |
|
34 |
|
35 def test_get_binary(self): |
|
36 """ Test mozinstall's get_binary method """ |
|
37 |
|
38 if mozinfo.isLinux: |
|
39 installdir = mozinstall.install(self.bz2, self.tempdir) |
|
40 binary = os.path.join(installdir, 'firefox') |
|
41 self.assertEqual(binary, mozinstall.get_binary(installdir, 'firefox')) |
|
42 |
|
43 elif mozinfo.isWin: |
|
44 installdir_exe = mozinstall.install(self.exe, |
|
45 os.path.join(self.tempdir, 'exe')) |
|
46 binary_exe = os.path.join(installdir_exe, 'firefox', 'firefox', |
|
47 'firefox.exe') |
|
48 self.assertEqual(binary_exe, mozinstall.get_binary(installdir_exe, |
|
49 'firefox')) |
|
50 |
|
51 installdir_zip = mozinstall.install(self.zipfile, |
|
52 os.path.join(self.tempdir, 'zip')) |
|
53 binary_zip = os.path.join(installdir_zip, 'firefox.exe') |
|
54 self.assertEqual(binary_zip, mozinstall.get_binary(installdir_zip, |
|
55 'firefox')) |
|
56 |
|
57 elif mozinfo.isMac: |
|
58 installdir = mozinstall.install(self.dmg, self.tempdir) |
|
59 binary = os.path.join(installdir, 'Contents', 'MacOS', 'firefox') |
|
60 self.assertEqual(binary, mozinstall.get_binary(installdir, 'firefox')) |
|
61 |
|
62 def test_get_binary_error(self): |
|
63 """ Test an InvalidBinary error is raised """ |
|
64 |
|
65 tempdir_empty = tempfile.mkdtemp() |
|
66 self.assertRaises(mozinstall.InvalidBinary, mozinstall.get_binary, |
|
67 tempdir_empty, 'firefox') |
|
68 mozfile.rmtree(tempdir_empty) |
|
69 |
|
70 def test_is_installer(self): |
|
71 """ Test we can identify a correct installer """ |
|
72 |
|
73 if mozinfo.isLinux: |
|
74 self.assertTrue(mozinstall.is_installer(self.bz2)) |
|
75 |
|
76 if mozinfo.isWin: |
|
77 # test zip installer |
|
78 self.assertTrue(mozinstall.is_installer(self.zipfile)) |
|
79 |
|
80 # test exe installer |
|
81 self.assertTrue(mozinstall.is_installer(self.exe)) |
|
82 |
|
83 try: |
|
84 # test stub browser file |
|
85 # without pefile on the system this test will fail |
|
86 import pefile |
|
87 stub_exe = os.path.join(here, 'build_stub', 'firefox.exe') |
|
88 self.assertFalse(mozinstall.is_installer(stub_exe)) |
|
89 except ImportError: |
|
90 pass |
|
91 |
|
92 if mozinfo.isMac: |
|
93 self.assertTrue(mozinstall.is_installer(self.dmg)) |
|
94 |
|
95 def test_invalid_source_error(self): |
|
96 """ Test InvalidSource error is raised with an incorrect installer """ |
|
97 |
|
98 if mozinfo.isLinux: |
|
99 self.assertRaises(mozinstall.InvalidSource, mozinstall.install, |
|
100 self.dmg, 'firefox') |
|
101 |
|
102 elif mozinfo.isWin: |
|
103 self.assertRaises(mozinstall.InvalidSource, mozinstall.install, |
|
104 self.bz2, 'firefox') |
|
105 |
|
106 elif mozinfo.isMac: |
|
107 self.assertRaises(mozinstall.InvalidSource, mozinstall.install, |
|
108 self.exe, 'firefox') |
|
109 |
|
110 def test_install(self): |
|
111 """ Test mozinstall's install capability """ |
|
112 |
|
113 if mozinfo.isLinux: |
|
114 installdir = mozinstall.install(self.bz2, self.tempdir) |
|
115 self.assertEqual(os.path.join(self.tempdir, 'firefox'), installdir) |
|
116 |
|
117 elif mozinfo.isWin: |
|
118 installdir_exe = mozinstall.install(self.exe, |
|
119 os.path.join(self.tempdir, 'exe')) |
|
120 self.assertEqual(os.path.join(self.tempdir, 'exe', 'firefox'), |
|
121 installdir_exe) |
|
122 |
|
123 installdir_zip = mozinstall.install(self.zipfile, |
|
124 os.path.join(self.tempdir, 'zip')) |
|
125 self.assertEqual(os.path.join(self.tempdir, 'zip', 'firefox'), |
|
126 installdir_zip) |
|
127 |
|
128 elif mozinfo.isMac: |
|
129 installdir = mozinstall.install(self.dmg, self.tempdir) |
|
130 self.assertEqual(os.path.join(os.path.realpath(self.tempdir), |
|
131 'FirefoxStub.app'), installdir) |
|
132 |
|
133 def test_uninstall(self): |
|
134 """ Test mozinstall's uninstall capabilites """ |
|
135 # Uninstall after installing |
|
136 |
|
137 if mozinfo.isLinux: |
|
138 installdir = mozinstall.install(self.bz2, self.tempdir) |
|
139 mozinstall.uninstall(installdir) |
|
140 self.assertFalse(os.path.exists(installdir)) |
|
141 |
|
142 elif mozinfo.isWin: |
|
143 # Exe installer for Windows |
|
144 installdir_exe = mozinstall.install(self.exe, |
|
145 os.path.join(self.tempdir, 'exe')) |
|
146 mozinstall.uninstall(installdir_exe) |
|
147 self.assertFalse(os.path.exists(installdir_exe)) |
|
148 |
|
149 # Zip installer for Windows |
|
150 installdir_zip = mozinstall.install(self.zipfile, |
|
151 os.path.join(self.tempdir, 'zip')) |
|
152 mozinstall.uninstall(installdir_zip) |
|
153 self.assertFalse(os.path.exists(installdir_zip)) |
|
154 |
|
155 elif mozinfo.isMac: |
|
156 installdir = mozinstall.install(self.dmg, self.tempdir) |
|
157 mozinstall.uninstall(installdir) |
|
158 self.assertFalse(os.path.exists(installdir)) |
|
159 |
|
160 if __name__ == '__main__': |
|
161 unittest.main() |