|
| 1 | +# Copyright (c) 2016-2017 Adobe Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all |
| 11 | +# copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +import os |
| 22 | +import sys |
| 23 | +import pytest |
| 24 | +import pkg_resources |
| 25 | +from user_sync import resource |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def resource_file(): |
| 30 | + """ |
| 31 | + Create an empty resource file |
| 32 | + :return: |
| 33 | + """ |
| 34 | + def _resource_file(dirname, filename): |
| 35 | + filepath = os.path.join(dirname, filename) |
| 36 | + open(filepath, 'a').close() |
| 37 | + return filepath |
| 38 | + return _resource_file |
| 39 | + |
| 40 | + |
| 41 | +def test_resource_file_bundle(resource_file, tmpdir, monkeypatch): |
| 42 | + """test for valid resource file in an EXE bundle""" |
| 43 | + tmpdir = str(tmpdir) |
| 44 | + with monkeypatch.context() as m: |
| 45 | + m.setattr(resource, '_run_context', None, False) |
| 46 | + m.setattr(sys, 'frozen', True, False) |
| 47 | + m.setattr(sys, '_MEIPASS', tmpdir, False) |
| 48 | + rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR) |
| 49 | + os.mkdir(rootdir) |
| 50 | + |
| 51 | + resfile = "test.txt" |
| 52 | + assert resource_file(rootdir, resfile) == resource.get_resource(resfile) |
| 53 | + |
| 54 | + |
| 55 | +def test_resource_file_package(resource_file, tmpdir, monkeypatch): |
| 56 | + """test for valid resource file in a Package context""" |
| 57 | + tmpdir = str(tmpdir) |
| 58 | + with monkeypatch.context() as m: |
| 59 | + resfile = "test.txt" |
| 60 | + m.setattr(pkg_resources, "resource_filename", lambda *args: os.path.join(tmpdir, resfile)) |
| 61 | + assert resource_file(tmpdir, resfile) == resource.get_resource(resfile) |
| 62 | + |
| 63 | + |
| 64 | +def test_resource_invalid_file_bundle(tmpdir, monkeypatch): |
| 65 | + """test for non-existent resource file in a bundle""" |
| 66 | + tmpdir = str(tmpdir) |
| 67 | + with monkeypatch.context() as m: |
| 68 | + m.setattr(resource, '_run_context', None, False) |
| 69 | + m.setattr(sys, 'frozen', True, False) |
| 70 | + m.setattr(sys, '_MEIPASS', tmpdir, False) |
| 71 | + |
| 72 | + rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR) |
| 73 | + os.mkdir(rootdir) |
| 74 | + |
| 75 | + resfile = "test.txt" |
| 76 | + assert resource.get_resource(resfile) is None |
| 77 | + |
| 78 | + |
| 79 | +def test_resource_invalid_file_package(tmpdir, monkeypatch): |
| 80 | + """test for non-existent resource file in a package""" |
| 81 | + tmpdir = str(tmpdir) |
| 82 | + with monkeypatch.context() as m: |
| 83 | + m.setattr(pkg_resources, "resource_filename", lambda *args: os.path.join('invalid', 'file', 'path')) |
| 84 | + resfile = os.path.join('invalid', 'file', 'path') |
| 85 | + assert resource.get_resource(resfile) is None |
| 86 | + |
| 87 | + |
| 88 | +def test_resource_dir_bundle(resource_file, tmpdir, monkeypatch): |
| 89 | + """test for valid resource files in directory in standalone run context""" |
| 90 | + tmpdir = str(tmpdir) |
| 91 | + with monkeypatch.context() as m: |
| 92 | + m.setattr(resource, '_run_context', None, False) |
| 93 | + m.setattr(sys, 'frozen', True, False) |
| 94 | + m.setattr(sys, '_MEIPASS', tmpdir, False) |
| 95 | + |
| 96 | + rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR) |
| 97 | + os.mkdir(rootdir) |
| 98 | + |
| 99 | + test_dir = os.path.join(rootdir, "test") |
| 100 | + os.mkdir(test_dir) |
| 101 | + |
| 102 | + resfile = "test_{}.txt" |
| 103 | + |
| 104 | + res_paths = [resource_file(test_dir, resfile.format(n+1)) for n in range(3)] |
| 105 | + |
| 106 | + assert sorted(res_paths) == sorted(resource.get_resource_dir('test')) |
| 107 | + |
| 108 | + |
| 109 | +def test_resource_dir_package(resource_file, tmpdir, monkeypatch): |
| 110 | + """test for valid resource files in directory in package run context""" |
| 111 | + tmpdir = str(tmpdir) |
| 112 | + with monkeypatch.context() as m: |
| 113 | + test_dir = os.path.join(tmpdir, "test") |
| 114 | + os.mkdir(test_dir) |
| 115 | + |
| 116 | + m.setattr(pkg_resources, "resource_filename", lambda *args: os.path.join(tmpdir, test_dir)) |
| 117 | + |
| 118 | + resfile = "test_{}.txt" |
| 119 | + |
| 120 | + res_test_files = [resfile.format(n+1) for n in range(3)] |
| 121 | + |
| 122 | + m.setattr(pkg_resources, "resource_listdir", lambda *args: res_test_files) |
| 123 | + |
| 124 | + res_paths = [resource_file(test_dir, resfile.format(n+1)) for n in range(3)] |
| 125 | + |
| 126 | + assert sorted(res_paths) == sorted(resource.get_resource_dir('test')) |
| 127 | + |
| 128 | + |
| 129 | +def test_resource_dir_empty(tmpdir, monkeypatch): |
| 130 | + """test for empty resource directory""" |
| 131 | + tmpdir = str(tmpdir) |
| 132 | + with monkeypatch.context() as m: |
| 133 | + m.setattr(resource, '_run_context', None, False) |
| 134 | + m.setattr(sys, 'frozen', True, False) |
| 135 | + m.setattr(sys, '_MEIPASS', tmpdir, False) |
| 136 | + |
| 137 | + rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR) |
| 138 | + os.mkdir(rootdir) |
| 139 | + |
| 140 | + test_dir = os.path.join(rootdir, "test") |
| 141 | + os.mkdir(test_dir) |
| 142 | + |
| 143 | + assert [] == resource.get_resource_dir('test') |
| 144 | + |
| 145 | + |
| 146 | +def test_resource_dir_invalid(tmpdir, monkeypatch): |
| 147 | + """test for nonexistent resource directory""" |
| 148 | + tmpdir = str(tmpdir) |
| 149 | + with monkeypatch.context() as m: |
| 150 | + m.setattr(resource, '_run_context', None, False) |
| 151 | + m.setattr(sys, 'frozen', True, False) |
| 152 | + m.setattr(sys, '_MEIPASS', tmpdir, False) |
| 153 | + rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR) |
| 154 | + os.mkdir(rootdir) |
| 155 | + |
| 156 | + with pytest.raises(AssertionError): |
| 157 | + resource.get_resource_dir('test') |
0 commit comments