Skip to content

Commit 53e093a

Browse files
authored
add expand_vars flag (#293)
* add expand_vars flag * added test
1 parent b752134 commit 53e093a

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [2.4.7] - Unreleased
9+
10+
### Added
11+
12+
- Flag to OSFS to disable env var expansion
13+
814
## [2.4.6] - 2019-06-08
915

1016
### Added

fs/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Version, used in module and setup.py.
22
"""
3-
__version__ = "2.4.6"
3+
__version__ = "2.4.7"

fs/osfs.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class OSFS(FS):
9191
create_mode (int): The permissions that will be used to create
9292
the directory if ``create`` is `True` and the path doesn't
9393
exist, defaults to ``0o777``.
94+
expand_vars(bool): If `True` (the default) environment variables of
95+
the form $name or ${name} will be expanded.
9496
9597
Raises:
9698
`fs.errors.CreateFailed`: If ``root_path`` does not
@@ -108,6 +110,7 @@ def __init__(
108110
root_path, # type: Text
109111
create=False, # type: bool
110112
create_mode=0o777, # type: SupportsInt
113+
expand_vars=True, # type: bool
111114
):
112115
# type: (...) -> None
113116
"""Create an OSFS instance.
@@ -118,7 +121,9 @@ def __init__(
118121
self.root_path = root_path
119122
_drive, _root_path = os.path.splitdrive(fsdecode(fspath(root_path)))
120123
_root_path = _drive + (_root_path or "/") if _drive else _root_path
121-
_root_path = os.path.expanduser(os.path.expandvars(_root_path))
124+
_root_path = os.path.expanduser(
125+
os.path.expandvars(_root_path) if expand_vars else _root_path
126+
)
122127
_root_path = os.path.normpath(os.path.abspath(_root_path))
123128
self._root_path = _root_path
124129

tests/test_osfs.py

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ def test_not_exists(self):
6969
with self.assertRaises(errors.CreateFailed):
7070
fs = osfs.OSFS("/does/not/exists/")
7171

72+
def test_expand_vars(self):
73+
self.fs.makedir("TYRIONLANISTER")
74+
self.fs.makedir("$FOO")
75+
path = self.fs.getsyspath("$FOO")
76+
os.environ["FOO"] = "TYRIONLANISTER"
77+
fs1 = osfs.OSFS(path)
78+
fs2 = osfs.OSFS(path, expand_vars=False)
79+
self.assertIn("TYRIONLANISTER", fs1.getsyspath("/"))
80+
self.assertNotIn("TYRIONLANISTER", fs2.getsyspath("/"))
81+
7282
@unittest.skipIf(osfs.sendfile is None, "sendfile not supported")
7383
def test_copy_sendfile(self):
7484
# try copying using sendfile

0 commit comments

Comments
 (0)