Coverage for tests/test_remote_test.py: 100%
58 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 02:14 -0700
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 02:14 -0700
1# This file is part of lsst-resources.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import os
13import unittest
15from lsst.resources import ResourcePath
16from lsst.resources.tests import GenericReadWriteTestCase, GenericTestCase, make_remote_test_uri
17from lsst.resources.utils import makeTestTempDir, removeTestTempDir
19TESTDIR = os.path.abspath(os.path.dirname(__file__))
22class RemoteTestTestCase(GenericTestCase, unittest.TestCase):
23 """File-specific generic test cases."""
25 scheme = "remote-test"
26 netloc = "localhost"
29class RemoteTestReadWriteTestCase(GenericReadWriteTestCase, unittest.TestCase):
30 """File tests involving reading and writing of data."""
32 scheme = "remote-test"
33 netloc = "localhost"
34 testdir = TESTDIR
35 # transfer_modes deliberately left at the remote default of copy/move.
36 # Link modes are refused for a non-local resource, which is the whole
37 # point of this scheme.
39 @classmethod
40 def setUpClass(cls) -> None:
41 # This scheme is backed by the local file system, so the URI path has
42 # to name a real writable directory. The netloc is not used.
43 cls._tmproot = makeTestTempDir(TESTDIR)
44 # Root the tests below a "+" so that every test exercises a path that
45 # has to be percent-encoded. An EUPS build directory embeds the
46 # package version in its name, so this really happens.
47 root = os.path.join(cls._tmproot, "resources-g1234567890+abcdef1234")
48 os.makedirs(root)
49 # base_path must be percent-encoded, so derive it from a URI rather
50 # than using the OS path directly.
51 cls.base_path = make_remote_test_uri(root).path.rstrip("/")
52 super().setUpClass()
54 @classmethod
55 def tearDownClass(cls) -> None:
56 removeTestTempDir(cls._tmproot)
57 super().tearDownClass()
59 def test_not_local(self) -> None:
60 """Test that the resource is not local and localizes to temp."""
61 test_file = self.root_uri.join("test_file.txt")
62 test_file.write(b"abc")
64 self.assertEqual(test_file.scheme, "remote-test")
65 self.assertEqual(test_file.read(), b"abc")
66 self.assertFalse(test_file.isLocal)
68 with test_file.as_local() as loc:
69 # Tests that ospath does not raise and that the "local" version
70 # of the file is at a different location.
71 self.assertNotEqual(loc.ospath, test_file.ospath)
74class RemoteTestUriTestCase(unittest.TestCase):
75 """Tests for the remote-test URI helper."""
77 def setUp(self):
78 self.root = makeTestTempDir(TESTDIR)
80 def tearDown(self):
81 removeTestTempDir(self.root)
83 def testScheme(self):
84 uri = make_remote_test_uri(self.root)
85 self.assertEqual(uri.scheme, "remote-test")
86 self.assertFalse(uri.isLocal)
87 self.assertEqual(uri.ospath.rstrip("/"), self.root)
89 def testSpecialCharacters(self):
90 awkward = os.path.join(self.root, "a dir with spaces")
91 os.makedirs(awkward)
92 uri = make_remote_test_uri(awkward)
94 # Interpolating the path into a string that already has a scheme
95 # leaves the spaces unencoded, so the helper has to encode them.
96 naive = ResourcePath(f"remote-test://localhost{awkward}/", forceDirectory=True)
97 self.assertIn(" ", naive.geturl())
98 self.assertNotIn(" ", uri.geturl())
100 self.assertEqual(uri.ospath.rstrip("/"), awkward)
102 child = uri.join("file.json", forceDirectory=False)
103 child.write(b"{}")
104 self.assertEqual(child.read(), b"{}")
106 def testFile(self):
107 uri = make_remote_test_uri(os.path.join(self.root, "a file.json"), forceDirectory=False)
108 self.assertFalse(uri.isdir())
109 self.assertIn("a%20file.json", uri.geturl())
110 self.assertTrue(uri.ospath.endswith("a file.json"))
113if __name__ == "__main__":
114 unittest.main()