tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test.py (5187B)


      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 json
      8 import os
      9 import sys
     10 from importlib import reload
     11 from unittest import mock
     12 
     13 import mozinfo
     14 import mozunit
     15 import pytest
     16 
     17 
     18 @pytest.fixture(autouse=True)
     19 def on_every_test():
     20    # per-test set up
     21    reload(mozinfo)
     22 
     23    # When running from an objdir mozinfo will use a build generated json file
     24    # instead of the ones created for testing. Prevent that from happening.
     25    # See bug 896038 for details.
     26    sys.modules["mozbuild"] = None
     27 
     28    yield
     29 
     30    # per-test tear down
     31    del sys.modules["mozbuild"]
     32 
     33 
     34 def test_basic():
     35    """Test that mozinfo has a few attributes."""
     36    assert mozinfo.os is not None
     37    # should have isFoo == True where os == "foo"
     38    assert getattr(mozinfo, "is" + mozinfo.os[0].upper() + mozinfo.os[1:])
     39 
     40 
     41 def test_update():
     42    """Test that mozinfo.update works."""
     43    mozinfo.update({"foo": 123})
     44    assert mozinfo.info["foo"] == 123
     45 
     46 
     47 def test_update_file(tmpdir):
     48    """Test that mozinfo.update can load a JSON file."""
     49    j = os.path.join(tmpdir, "mozinfo.json")
     50    with open(j, "w") as f:
     51        f.write(json.dumps({"foo": "xyz"}))
     52    mozinfo.update(j)
     53    assert mozinfo.info["foo"] == "xyz"
     54 
     55 
     56 def test_update_file_invalid_json(tmpdir):
     57    """Test that mozinfo.update handles invalid JSON correctly"""
     58    j = os.path.join(tmpdir, "test.json")
     59    with open(j, "w") as f:
     60        f.write('invalid{"json":')
     61    with pytest.raises(ValueError):
     62        mozinfo.update([j])
     63 
     64 
     65 def test_find_and_update_file(tmpdir):
     66    """Test that mozinfo.find_and_update_from_json can
     67    find mozinfo.json in a directory passed to it."""
     68    j = os.path.join(tmpdir, "mozinfo.json")
     69    with open(j, "w") as f:
     70        f.write(json.dumps({"foo": "abcdefg"}))
     71    assert mozinfo.find_and_update_from_json(tmpdir) == j
     72    assert mozinfo.info["foo"] == "abcdefg"
     73 
     74 
     75 def test_find_and_update_file_no_argument():
     76    """Test that mozinfo.find_and_update_from_json no-ops on not being
     77    given any arguments.
     78    """
     79    assert mozinfo.find_and_update_from_json() is None
     80 
     81 
     82 def test_find_and_update_file_invalid_json(tmpdir):
     83    """Test that mozinfo.find_and_update_from_json can
     84    handle invalid JSON"""
     85    j = os.path.join(tmpdir, "mozinfo.json")
     86    with open(j, "w") as f:
     87        f.write('invalid{"json":')
     88    with pytest.raises(ValueError):
     89        mozinfo.find_and_update_from_json(tmpdir)
     90 
     91 
     92 def test_find_and_update_file_raise_exception():
     93    """Test that mozinfo.find_and_update_from_json raises
     94    an IOError when exceptions are unsuppressed.
     95    """
     96    with pytest.raises(IOError):
     97        mozinfo.find_and_update_from_json(raise_exception=True)
     98 
     99 
    100 def test_find_and_update_file_suppress_exception():
    101    """Test that mozinfo.find_and_update_from_json suppresses
    102    an IOError exception if a False boolean value is
    103    provided as the only argument.
    104    """
    105    assert mozinfo.find_and_update_from_json(raise_exception=False) is None
    106 
    107 
    108 def test_find_and_update_file_mozbuild(tmpdir):
    109    """Test that mozinfo.find_and_update_from_json can
    110    find mozinfo.json using the mozbuild module."""
    111    j = os.path.join(tmpdir, "mozinfo.json")
    112    with open(j, "w") as f:
    113        f.write(json.dumps({"foo": "123456"}))
    114    m = mock.MagicMock()
    115    # Mock the value of MozbuildObject.from_environment().topobjdir.
    116    m.MozbuildObject.from_environment.return_value.topobjdir = tmpdir
    117 
    118    mocked_modules = {
    119        "mozbuild": m,
    120        "mozbuild.base": m,
    121        "mozbuild.mozconfig": m,
    122    }
    123    with mock.patch.dict(sys.modules, mocked_modules):
    124        assert mozinfo.find_and_update_from_json() == j
    125    assert mozinfo.info["foo"] == "123456"
    126 
    127 
    128 def test_output_to_file(tmpdir):
    129    """Test that mozinfo.output_to_file works."""
    130    path = os.path.join(tmpdir, "mozinfo.json")
    131    mozinfo.output_to_file(path)
    132    assert open(path).read() == json.dumps(mozinfo.info)
    133 
    134 
    135 def test_os_version_is_a_StringVersion():
    136    assert isinstance(mozinfo.os_version, mozinfo.StringVersion)
    137 
    138 
    139 def test_compare_to_string():
    140    version = mozinfo.StringVersion("10.10")
    141 
    142    assert version > "10.2"
    143    assert "11" > version
    144    assert version >= "10.10"
    145    assert "10.11" >= version
    146    assert version == "10.10"
    147    assert "10.10" == version
    148    assert version != "10.2"
    149    assert "11" != version
    150    assert version < "11.8.5"
    151    assert "10.2" < version
    152    assert version <= "11"
    153    assert "10.10" <= version
    154 
    155    # Can have non-numeric versions (Bug 1654915)
    156    assert version != mozinfo.StringVersion("Testing")
    157    assert mozinfo.StringVersion("Testing") != version
    158    assert mozinfo.StringVersion("") == ""
    159    assert "" == mozinfo.StringVersion("")
    160 
    161    a = mozinfo.StringVersion("1.2.5a")
    162    b = mozinfo.StringVersion("1.2.5b")
    163    assert a < b
    164    assert b > a
    165 
    166    # Make sure we can compare against unicode (for python 2).
    167    assert a == "1.2.5a"
    168    assert "1.2.5a" == a
    169 
    170 
    171 def test_to_string():
    172    assert "10.10" == str(mozinfo.StringVersion("10.10"))
    173 
    174 
    175 if __name__ == "__main__":
    176    mozunit.main()