1#----------------------------------------------------------------------------- 2# Copyright (c) 2012 - 2021, Anaconda, Inc., and Bokeh Contributors. 3# All rights reserved. 4# 5# The full license is in the file LICENSE.txt, distributed with this software. 6#----------------------------------------------------------------------------- 7 8#----------------------------------------------------------------------------- 9# Boilerplate 10#----------------------------------------------------------------------------- 11import pytest ; pytest 12 13#----------------------------------------------------------------------------- 14# Imports 15#----------------------------------------------------------------------------- 16 17# Standard library imports 18import os 19 20# External imports 21from mock import Mock, PropertyMock, patch 22 23# Module under test 24import bokeh.io.util as biu # isort:skip 25 26#----------------------------------------------------------------------------- 27# Setup 28#----------------------------------------------------------------------------- 29 30#----------------------------------------------------------------------------- 31# General API 32#----------------------------------------------------------------------------- 33 34#----------------------------------------------------------------------------- 35# Dev API 36#----------------------------------------------------------------------------- 37 38def test_detect_current_filename() -> None: 39 assert biu.detect_current_filename().endswith(("py.test", "pytest", "py.test-script.py", "pytest-script.py")) 40 41@patch('bokeh.io.util.NamedTemporaryFile') 42def test_temp_filename(mock_tmp) -> None: 43 fn = Mock() 44 type(fn).name = PropertyMock(return_value="Junk.test") 45 mock_tmp.return_value = fn 46 47 r = biu.temp_filename("test") 48 assert r == "Junk.test" 49 assert mock_tmp.called 50 assert mock_tmp.call_args[0] == () 51 assert mock_tmp.call_args[1] == {'suffix': '.test'} 52 53def test_default_filename() -> None: 54 old_detect_current_filename = biu.detect_current_filename 55 old__no_access = biu._no_access 56 old__shares_exec_prefix = biu._shares_exec_prefix 57 58 biu.detect_current_filename = lambda : "/a/b/foo.py" 59 60 try: 61 # .py extension 62 with pytest.raises(RuntimeError): 63 biu.default_filename("py") 64 65 # a current file, access, and no share exec 66 biu._no_access = lambda x: False 67 r = biu.default_filename("test") 68 assert os.path.normpath(r) == os.path.normpath("/a/b/foo.test") 69 70 # a current file, NO access, and no share exec 71 biu._no_access = lambda x: True 72 r = biu.default_filename("test") 73 assert os.path.normpath(r) != os.path.normpath("/a/b/foo.test") 74 assert r.endswith(".test") 75 76 # a current file, access, but WITH share exec 77 biu._no_access = lambda x: False 78 biu._shares_exec_prefix = lambda x: True 79 r = biu.default_filename("test") 80 assert os.path.normpath(r) != os.path.normpath("/a/b/foo.test") 81 assert r.endswith(".test") 82 83 # no current file 84 biu.detect_current_filename = lambda : None 85 biu._no_access = lambda x: False 86 biu._shares_exec_prefix = lambda x: False 87 r = biu.default_filename("test") 88 assert os.path.normpath(r) != os.path.normpath("/a/b/foo.test") 89 assert r.endswith(".test") 90 91 finally: 92 biu.detect_current_filename = old_detect_current_filename 93 biu._no_access = old__no_access 94 biu._shares_exec_prefix = old__shares_exec_prefix 95 96#----------------------------------------------------------------------------- 97# Private API 98#----------------------------------------------------------------------------- 99 100@patch('os.access') 101def test__no_access(mock_access) -> None: 102 biu._no_access("test") 103 assert mock_access.called 104 assert mock_access.call_args[0] == ("test", os.W_OK | os.X_OK) 105 assert mock_access.call_args[1] == {} 106 107def test__shares_exec_prefix() -> None: 108 import sys 109 old_ex = sys.exec_prefix 110 try: 111 sys.exec_prefix = "/foo/bar" 112 assert biu._shares_exec_prefix("/foo/bar") == True 113 sys.exec_prefix = "/baz/bar" 114 assert biu._shares_exec_prefix("/foo/bar") == False 115 sys.exec_prefix = None 116 assert biu._shares_exec_prefix("/foo/bar") == False 117 finally: 118 sys.exec_prefix = old_ex 119 120#----------------------------------------------------------------------------- 121# Code 122#----------------------------------------------------------------------------- 123