github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/lib/tempfile_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  import os
    16  import stat
    17  import tempfile
    18  
    19  import weetest
    20  
    21  
    22  def TestMkdTemp():
    23    path = tempfile.mkdtemp()
    24    mode = os.stat(path).st_mode
    25    os.rmdir(path)
    26    assert stat.S_ISDIR(mode), mode
    27    assert stat.S_IMODE(mode) == 0o700, mode
    28  
    29  
    30  def TestMkdTempDir():
    31    tempdir = tempfile.mkdtemp()
    32    path = tempfile.mkdtemp(dir=tempdir)
    33    os.rmdir(path)
    34    os.rmdir(tempdir)
    35    assert path.startswith(tempdir)
    36  
    37  
    38  def TestMkdTempOSError():
    39    tempdir = tempfile.mkdtemp()
    40    os.chmod(tempdir, 0o500)
    41  
    42    if os.geteuid() == 0:
    43      print ('Warning: Cannot reliable test file readonly-ness with Root user')
    44      mode = os.stat(tempdir).st_mode
    45      assert stat.S_IMODE(mode) == 0o500, ('Wrong file mode "%s" detected' % mode)
    46  
    47    else:
    48      try:
    49        tempfile.mkdtemp(dir=tempdir)
    50      except OSError:
    51        pass
    52      else:
    53        raise AssertionError, 'Should not be able to touch 0o500 paths'
    54    os.rmdir(tempdir)
    55  
    56  
    57  def TestMkdTempPrefixSuffix():
    58    path = tempfile.mkdtemp(prefix='foo', suffix='bar')
    59    os.rmdir(path)
    60    assert 'foo' in path
    61    assert 'bar' in path
    62    # TODO: assert path.endswith('bar')
    63  
    64  
    65  def TestMksTemp():
    66    fd, path = tempfile.mkstemp()
    67    f = os.fdopen(fd, 'w')
    68    f.write('foobar')
    69    f.close()
    70    f = open(path)
    71    contents = f.read()
    72    f.close()
    73    os.remove(path)
    74    assert contents == 'foobar', contents
    75  
    76  
    77  def TestMksTempDir():
    78    tempdir = tempfile.mkdtemp()
    79    fd, path = tempfile.mkstemp(dir=tempdir)
    80    os.close(fd)
    81    os.remove(path)
    82    os.rmdir(tempdir)
    83    assert path.startswith(tempdir)
    84  
    85  
    86  def TestMksTempOSError():
    87    tempdir = tempfile.mkdtemp()
    88    os.chmod(tempdir, 0o500)
    89  
    90    if os.geteuid() == 0:
    91      print ('Warning: Cannot reliable test file readonly-ness with Root user')
    92      mode = os.stat(tempdir).st_mode
    93      assert stat.S_IMODE(mode) == 0o500, ('Wrong file mode "%s" detected' % mode)
    94  
    95    else:
    96      try:
    97        tempfile.mkstemp(dir=tempdir)
    98      except OSError:
    99        pass
   100      else:
   101        raise AssertionError
   102    os.rmdir(tempdir)
   103  
   104  
   105  def TestMksTempPerms():
   106    fd, path = tempfile.mkstemp()
   107    os.close(fd)
   108    mode = os.stat(path).st_mode
   109    os.remove(path)
   110    assert stat.S_IMODE(mode) == 0o600, mode
   111  
   112  
   113  def TestMksTempPrefixSuffix():
   114    fd, path = tempfile.mkstemp(prefix='foo', suffix='bar')
   115    os.close(fd)
   116    os.remove(path)
   117    assert 'foo' in path
   118    assert 'bar' in path
   119    # TODO: assert path.endswith('bar')
   120  
   121  
   122  if __name__ == '__main__':
   123    weetest.RunTests()