1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozinstall/tests/test.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# This Source Code Form is subject to the terms of the Mozilla Public 1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 +# You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + 1.10 +import mozinfo 1.11 +import mozinstall 1.12 +import mozfile 1.13 +import os 1.14 +import tarfile 1.15 +import tempfile 1.16 +import unittest 1.17 +import zipfile 1.18 + 1.19 +# Store file location at load time 1.20 +here = os.path.dirname(os.path.abspath(__file__)) 1.21 + 1.22 +class TestMozInstall(unittest.TestCase): 1.23 + 1.24 + @classmethod 1.25 + def setUpClass(cls): 1.26 + """ Setting up stub installers """ 1.27 + cls.dmg = os.path.join(here, 'Installer-Stubs', 'firefox.dmg') 1.28 + cls.exe = os.path.join(here, 'Installer-Stubs', 'firefox.exe') 1.29 + cls.zipfile = os.path.join(here, 'Installer-Stubs', 'firefox.zip') 1.30 + cls.bz2 = os.path.join(here, 'Installer-Stubs', 'firefox.tar.bz2') 1.31 + 1.32 + def setUp(self): 1.33 + self.tempdir = tempfile.mkdtemp() 1.34 + 1.35 + def tearDown(self): 1.36 + mozfile.rmtree(self.tempdir) 1.37 + 1.38 + def test_get_binary(self): 1.39 + """ Test mozinstall's get_binary method """ 1.40 + 1.41 + if mozinfo.isLinux: 1.42 + installdir = mozinstall.install(self.bz2, self.tempdir) 1.43 + binary = os.path.join(installdir, 'firefox') 1.44 + self.assertEqual(binary, mozinstall.get_binary(installdir, 'firefox')) 1.45 + 1.46 + elif mozinfo.isWin: 1.47 + installdir_exe = mozinstall.install(self.exe, 1.48 + os.path.join(self.tempdir, 'exe')) 1.49 + binary_exe = os.path.join(installdir_exe, 'firefox', 'firefox', 1.50 + 'firefox.exe') 1.51 + self.assertEqual(binary_exe, mozinstall.get_binary(installdir_exe, 1.52 + 'firefox')) 1.53 + 1.54 + installdir_zip = mozinstall.install(self.zipfile, 1.55 + os.path.join(self.tempdir, 'zip')) 1.56 + binary_zip = os.path.join(installdir_zip, 'firefox.exe') 1.57 + self.assertEqual(binary_zip, mozinstall.get_binary(installdir_zip, 1.58 + 'firefox')) 1.59 + 1.60 + elif mozinfo.isMac: 1.61 + installdir = mozinstall.install(self.dmg, self.tempdir) 1.62 + binary = os.path.join(installdir, 'Contents', 'MacOS', 'firefox') 1.63 + self.assertEqual(binary, mozinstall.get_binary(installdir, 'firefox')) 1.64 + 1.65 + def test_get_binary_error(self): 1.66 + """ Test an InvalidBinary error is raised """ 1.67 + 1.68 + tempdir_empty = tempfile.mkdtemp() 1.69 + self.assertRaises(mozinstall.InvalidBinary, mozinstall.get_binary, 1.70 + tempdir_empty, 'firefox') 1.71 + mozfile.rmtree(tempdir_empty) 1.72 + 1.73 + def test_is_installer(self): 1.74 + """ Test we can identify a correct installer """ 1.75 + 1.76 + if mozinfo.isLinux: 1.77 + self.assertTrue(mozinstall.is_installer(self.bz2)) 1.78 + 1.79 + if mozinfo.isWin: 1.80 + # test zip installer 1.81 + self.assertTrue(mozinstall.is_installer(self.zipfile)) 1.82 + 1.83 + # test exe installer 1.84 + self.assertTrue(mozinstall.is_installer(self.exe)) 1.85 + 1.86 + try: 1.87 + # test stub browser file 1.88 + # without pefile on the system this test will fail 1.89 + import pefile 1.90 + stub_exe = os.path.join(here, 'build_stub', 'firefox.exe') 1.91 + self.assertFalse(mozinstall.is_installer(stub_exe)) 1.92 + except ImportError: 1.93 + pass 1.94 + 1.95 + if mozinfo.isMac: 1.96 + self.assertTrue(mozinstall.is_installer(self.dmg)) 1.97 + 1.98 + def test_invalid_source_error(self): 1.99 + """ Test InvalidSource error is raised with an incorrect installer """ 1.100 + 1.101 + if mozinfo.isLinux: 1.102 + self.assertRaises(mozinstall.InvalidSource, mozinstall.install, 1.103 + self.dmg, 'firefox') 1.104 + 1.105 + elif mozinfo.isWin: 1.106 + self.assertRaises(mozinstall.InvalidSource, mozinstall.install, 1.107 + self.bz2, 'firefox') 1.108 + 1.109 + elif mozinfo.isMac: 1.110 + self.assertRaises(mozinstall.InvalidSource, mozinstall.install, 1.111 + self.exe, 'firefox') 1.112 + 1.113 + def test_install(self): 1.114 + """ Test mozinstall's install capability """ 1.115 + 1.116 + if mozinfo.isLinux: 1.117 + installdir = mozinstall.install(self.bz2, self.tempdir) 1.118 + self.assertEqual(os.path.join(self.tempdir, 'firefox'), installdir) 1.119 + 1.120 + elif mozinfo.isWin: 1.121 + installdir_exe = mozinstall.install(self.exe, 1.122 + os.path.join(self.tempdir, 'exe')) 1.123 + self.assertEqual(os.path.join(self.tempdir, 'exe', 'firefox'), 1.124 + installdir_exe) 1.125 + 1.126 + installdir_zip = mozinstall.install(self.zipfile, 1.127 + os.path.join(self.tempdir, 'zip')) 1.128 + self.assertEqual(os.path.join(self.tempdir, 'zip', 'firefox'), 1.129 + installdir_zip) 1.130 + 1.131 + elif mozinfo.isMac: 1.132 + installdir = mozinstall.install(self.dmg, self.tempdir) 1.133 + self.assertEqual(os.path.join(os.path.realpath(self.tempdir), 1.134 + 'FirefoxStub.app'), installdir) 1.135 + 1.136 + def test_uninstall(self): 1.137 + """ Test mozinstall's uninstall capabilites """ 1.138 + # Uninstall after installing 1.139 + 1.140 + if mozinfo.isLinux: 1.141 + installdir = mozinstall.install(self.bz2, self.tempdir) 1.142 + mozinstall.uninstall(installdir) 1.143 + self.assertFalse(os.path.exists(installdir)) 1.144 + 1.145 + elif mozinfo.isWin: 1.146 + # Exe installer for Windows 1.147 + installdir_exe = mozinstall.install(self.exe, 1.148 + os.path.join(self.tempdir, 'exe')) 1.149 + mozinstall.uninstall(installdir_exe) 1.150 + self.assertFalse(os.path.exists(installdir_exe)) 1.151 + 1.152 + # Zip installer for Windows 1.153 + installdir_zip = mozinstall.install(self.zipfile, 1.154 + os.path.join(self.tempdir, 'zip')) 1.155 + mozinstall.uninstall(installdir_zip) 1.156 + self.assertFalse(os.path.exists(installdir_zip)) 1.157 + 1.158 + elif mozinfo.isMac: 1.159 + installdir = mozinstall.install(self.dmg, self.tempdir) 1.160 + mozinstall.uninstall(installdir) 1.161 + self.assertFalse(os.path.exists(installdir)) 1.162 + 1.163 +if __name__ == '__main__': 1.164 + unittest.main()