-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[External] [stdlib] Add
os.path.splitroot
(#51325)
[External] [stdlib] Add `os.path.splitroot` Also cleaned up a previous changelog entry so it aligns with the function's intent. Co-authored-by: Mikhail Tavarez <[email protected]> Closes #3780 MODULAR_ORIG_COMMIT_REV_ID: 081959627df8f25d7f6e04856ca03ab992ecca6c
- Loading branch information
1 parent
7a503da
commit ced5633
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,6 @@ from .path import ( | |
islink, | ||
join, | ||
split, | ||
splitroot, | ||
lexists, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2024, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
# RUN: %mojo %s | ||
|
||
import os | ||
from os.path import splitroot | ||
from testing import assert_equal | ||
|
||
|
||
def test_absolute_path(): | ||
drive, root, tail = splitroot("/usr/lib/file.txt") | ||
assert_equal(drive, "") | ||
assert_equal(root, "/") | ||
assert_equal(tail, "usr/lib/file.txt") | ||
|
||
drive, root, tail = splitroot("//usr/lib/file.txt") | ||
assert_equal(drive, "") | ||
assert_equal(root, "//") | ||
assert_equal(tail, "usr/lib/file.txt") | ||
|
||
drive, root, tail = splitroot("///usr/lib/file.txt") | ||
assert_equal(drive, "") | ||
assert_equal(root, "/") | ||
assert_equal(tail, "//usr/lib/file.txt") | ||
|
||
|
||
def test_relative_path(): | ||
drive, root, tail = splitroot("usr/lib/file.txt") | ||
assert_equal(drive, "") | ||
assert_equal(root, "") | ||
assert_equal(tail, "usr/lib/file.txt") | ||
|
||
drive, root, tail = splitroot(".") | ||
assert_equal(drive, "") | ||
assert_equal(root, "") | ||
assert_equal(tail, ".") | ||
|
||
drive, root, tail = splitroot("..") | ||
assert_equal(drive, "") | ||
assert_equal(root, "") | ||
assert_equal(tail, "..") | ||
|
||
drive, root, tail = splitroot("entire/.//.tail/..//captured////") | ||
assert_equal(drive, "") | ||
assert_equal(root, "") | ||
assert_equal(tail, "entire/.//.tail/..//captured////") | ||
|
||
|
||
def test_root_directory(): | ||
drive, root, tail = splitroot("/") | ||
assert_equal(drive, "") | ||
assert_equal(root, "/") | ||
assert_equal(tail, "") | ||
|
||
drive, root, tail = splitroot("//") | ||
assert_equal(drive, "") | ||
assert_equal(root, "//") | ||
assert_equal(tail, "") | ||
|
||
drive, root, tail = splitroot("///") | ||
assert_equal(drive, "") | ||
assert_equal(root, "/") | ||
assert_equal(tail, "//") | ||
|
||
|
||
def test_empty_path(): | ||
drive, root, tail = splitroot("") | ||
assert_equal(drive, "") | ||
assert_equal(root, "") | ||
assert_equal(tail, "") | ||
|
||
|
||
def main(): | ||
test_absolute_path() | ||
test_relative_path() | ||
test_root_directory() | ||
test_empty_path() |