-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_cache.py
More file actions
67 lines (54 loc) · 2.54 KB
/
test_cache.py
File metadata and controls
67 lines (54 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# Sonar Scanner Python
# Copyright (C) 2011-2026 SonarSource Sàrl
# mailto:info AT sonarsource DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
#
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import pathlib
import unittest
import pyfakefs.fake_filesystem_unittest as pyfakefs
from pysonar_scanner.cache import Cache, CacheFile
import pysonar_scanner.cache as cache
from pysonar_scanner.configuration.properties import SONAR_USER_HOME
class TestCacheFile(unittest.TestCase):
def test_is_valid(self):
self.assertFalse(CacheFile(pathlib.Path("/tmp/none-existing-file"), checksum="123").is_valid())
class TestCache(pyfakefs.TestCase):
def setUp(self):
self.setUpPyfakefs()
def test_if_cache_folder_is_created(self):
cache_path = pathlib.Path("/folder1/folder2/")
cache = Cache.create_cache(cache_path)
self.assertTrue(self.fs.exists(cache_path))
self.assertEqual(cache.cache_folder, cache_path)
def test_cache_constructor(self):
with self.assertRaises(FileNotFoundError):
Cache(pathlib.Path("/folder1/folder2/"))
def test_get_file(self):
cache = Cache.create_cache(pathlib.Path("/folder1/folder2/"))
cache_file = cache.get_file("test", "123")
self.assertEqual(cache_file.filepath, pathlib.Path("/folder1/folder2/test"))
self.assertEqual(cache_file.checksum, "123")
def test_get_default(self):
self.assertEqual(cache.get_cache({}).cache_folder, pathlib.Path.home() / ".sonar/cache")
def test_uses_user_home(self):
self.assertEqual(cache.get_cache({SONAR_USER_HOME: "my/home"}).cache_folder, pathlib.Path("my/home") / "cache")
def test_exists(self):
cache = Cache.create_cache(pathlib.Path("/folder1/folder2/"))
cache_file = cache.get_file("test", "123")
self.assertFalse(cache_file.exists())
cache_file.filepath.touch()
self.assertTrue(cache_file.exists())