github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/lib/os/path_test.py (about)

     1  # Copyright 2016 Google Inc. All Rights Reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  # pylint: disable=g-import-not-at-top
    16  
    17  import os
    18  import os.path
    19  path = os.path
    20  
    21  import weetest
    22  import tempfile
    23  
    24  
    25  def _AssertEqual(a, b):
    26    assert a == b
    27    assert type(a) is type(b)
    28  
    29  
    30  def TestAbspath():
    31    _AssertEqual(path.abspath('/a/b/c'), '/a/b/c')
    32    _AssertEqual(path.abspath(u'/a/b/c'), u'/a/b/c')
    33    _AssertEqual(path.abspath('/a/b/c/'), '/a/b/c')
    34    _AssertEqual(path.abspath(u'/a/b/c/'), u'/a/b/c')
    35    _AssertEqual(path.abspath('a/b/c'), path.normpath(os.getcwd() + '/a/b/c'))
    36  
    37  
    38  def TestBasename():
    39    assert path.basename('/a/b/c') == 'c'
    40    assert path.basename('/a/b/c/') == ''
    41  
    42  
    43  def TestDirname():
    44    assert path.dirname('/a/b/c') == '/a/b'
    45    assert path.dirname('/a/b/c/') == '/a/b/c'
    46  
    47  
    48  def TestExists():
    49    _, file_path = tempfile.mkstemp()
    50    dir_path = tempfile.mkdtemp()
    51    try:
    52      assert path.exists(file_path)
    53      assert path.exists(dir_path)
    54      assert not path.exists('path/does/not/exist')
    55    finally:
    56      os.remove(file_path)
    57      os.rmdir(dir_path)
    58  
    59  
    60  def TestIsAbs():
    61    assert path.isabs('/abc')
    62    assert not path.isabs('abc/123')
    63  
    64  
    65  def TestIsDir():
    66    _, file_path = tempfile.mkstemp()
    67    dir_path = tempfile.mkdtemp()
    68    try:
    69      assert not path.isdir(file_path)
    70      assert path.isdir(dir_path)
    71      assert not path.isdir('path/does/not/exist')
    72    finally:
    73      os.remove(file_path)
    74      os.rmdir(dir_path)
    75  
    76  
    77  def TestIsFile():
    78    _, file_path = tempfile.mkstemp()
    79    dir_path = tempfile.mkdtemp()
    80    try:
    81      assert path.isfile(file_path)
    82      assert not path.isfile(dir_path)
    83      assert not path.isfile('path/does/not/exist')
    84    finally:
    85      os.remove(file_path)
    86      os.rmdir(dir_path)
    87  
    88  
    89  def TestJoin():
    90    assert path.join('') == ''
    91    assert path.join('', '') == ''
    92    assert path.join('abc') == 'abc'
    93    assert path.join('abc', '') == 'abc/'
    94    assert path.join('abc', '', '') == 'abc/'
    95    assert path.join('abc', '', '123') == 'abc/123'
    96    assert path.normpath(path.join('abc', '.', '123')) == 'abc/123'
    97    assert path.normpath(path.join('abc', '..', '123')) == '123'
    98    assert path.join('/abc', '123') == '/abc/123'
    99    assert path.join('abc', '/123') == '/123'
   100    assert path.join('abc/', '123') == 'abc/123'
   101    assert path.join('abc', 'x/y/z') == 'abc/x/y/z'
   102    assert path.join('abc', 'x', 'y', 'z') == 'abc/x/y/z'
   103  
   104  
   105  def TestNormPath():
   106    _AssertEqual(path.normpath('abc/'), 'abc')
   107    _AssertEqual(path.normpath('/a//b'), '/a/b')
   108    _AssertEqual(path.normpath('abc/../123'), '123')
   109    _AssertEqual(path.normpath('../abc/123'), '../abc/123')
   110    _AssertEqual(path.normpath('x/y/./z'), 'x/y/z')
   111    _AssertEqual(path.normpath(u'abc/'), u'abc')
   112    _AssertEqual(path.normpath(u'/a//b'), u'/a/b')
   113    _AssertEqual(path.normpath(u'abc/../123'), u'123')
   114    _AssertEqual(path.normpath(u'../abc/123'), u'../abc/123')
   115    _AssertEqual(path.normpath(u'x/y/./z'), u'x/y/z')
   116  
   117  
   118  def TestSplit():
   119    assert path.split('a/b') == ('a', 'b')
   120    assert path.split('a/b/') == ('a/b', '')
   121    assert path.split('a/') == ('a', '')
   122    assert path.split('a') == ('', 'a')
   123    assert path.split('/') == ('/', '')
   124    assert path.split('/a/./b') == ('/a/.', 'b')
   125  
   126  
   127  if __name__ == '__main__':
   128    weetest.RunTests()