tor-browser

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

test_which.py (1947B)


      1 # Any copyright is dedicated to the Public Domain.
      2 # https://creativecommons.org/publicdomain/zero/1.0/
      3 
      4 import os
      5 import sys
      6 
      7 import mozunit
      8 from mozfile import which
      9 
     10 here = os.path.abspath(os.path.dirname(__file__))
     11 
     12 
     13 def test_which(monkeypatch):
     14    cwd = os.path.join(here, "files", "which")
     15    monkeypatch.chdir(cwd)
     16 
     17    if sys.platform == "win32":
     18        import winreg
     19 
     20        bindir = os.path.join(cwd, "win")
     21        monkeypatch.setenv("PATH", bindir)
     22        monkeypatch.setattr(winreg, "QueryValue", (lambda k, sk: None))
     23 
     24        assert which("foo.exe").lower() == os.path.join(bindir, "foo.exe").lower()
     25        assert which("foo").lower() == os.path.join(bindir, "foo.exe").lower()
     26        assert (
     27            which("foo", exts=[".FOO", ".BAR", "."]).lower()
     28            == os.path.join(bindir, "foo").lower()
     29        )
     30        assert os.environ.get("PATHEXT") != [".FOO", ".BAR"]
     31        assert which("foo.txt") is None
     32 
     33        assert which("bar", exts=["."]).lower() == os.path.join(bindir, "bar").lower()
     34        assert which("baz").lower() == os.path.join(cwd, "baz.exe").lower()
     35 
     36        registered_dir = os.path.join(cwd, "registered")
     37        quux = os.path.join(registered_dir, "quux.exe").lower()
     38 
     39        def mock_registry(key, subkey):
     40            assert subkey == (
     41                r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\quux.exe"
     42            )
     43            return quux
     44 
     45        monkeypatch.setattr(winreg, "QueryValue", mock_registry)
     46        assert which("quux").lower() == quux
     47        assert which("quux.exe").lower() == quux
     48 
     49    else:
     50        bindir = os.path.join(cwd, "unix")
     51        monkeypatch.setenv("PATH", bindir)
     52        assert which("foo") == os.path.join(bindir, "foo")
     53        assert which("baz") is None
     54        assert which("baz", exts=[".EXE"]) is None
     55        assert "PATHEXT" not in os.environ
     56        assert which("file") is None
     57 
     58 
     59 if __name__ == "__main__":
     60    mozunit.main()