1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
"""Tests for the file_path module.
"""
# Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
# Copyright (c) 2005, Enthought, Inc.
# License: BSD Style.
# Standard library imports.
import unittest
import os
import sys
from os.path import abspath, dirname, basename, join
from io import BytesIO
# Enthought library imports.
from apptools.persistence import state_pickler
from apptools.persistence import file_path
class Test:
def __init__(self):
self.f = file_path.FilePath()
class TestFilePath(unittest.TestCase):
def test_relative(self):
"""Test if relative paths are set correctly.
"""
fname = 't.vtk'
f = file_path.FilePath(fname)
cwd = os.getcwd()
# Trivial case of both in same dir.
f.set_relative(abspath(join(cwd, 't.mv2')))
self.assertEqual(f.rel_pth, fname)
# Move one directory deeper.
f.set_relative(abspath(join(cwd, 'tests', 't.mv2')))
self.assertEqual(f.rel_pth, join(os.pardir, fname))
# Move one directory shallower.
f.set_relative(abspath(join(dirname(cwd), 't.mv2')))
diff = basename(cwd)
self.assertEqual(f.rel_pth, join(diff, fname))
# Test where the path is relative to the root.
f.set(abspath(join('data', fname)))
f.set_relative('/tmp/test.mv2')
if sys.platform.startswith('win'):
expect = os.pardir + abspath(join('data', fname))[2:]
else:
expect = os.pardir + abspath(join('data', fname))
self.assertEqual(f.rel_pth, expect)
def test_absolute(self):
"""Test if absolute paths are set corectly.
"""
fname = 't.vtk'
f = file_path.FilePath(fname)
cwd = os.getcwd()
# Easy case of both in same dir.
f.set_absolute(join(cwd, 'foo', 'test', 't.mv2'))
self.assertEqual(f.abs_pth, join(cwd, 'foo', 'test', fname))
# One level lower.
fname = join(os.pardir, 't.vtk')
f.set(fname)
f.set_absolute(join(cwd, 'foo', 'test', 't.mv2'))
self.assertEqual(f.abs_pth, abspath(join(cwd, 'foo', 'test', fname)))
# One level higher.
fname = join('test', 't.vtk')
f.set(fname)
f.set_absolute(join(cwd, 'foo', 't.mv2'))
self.assertEqual(f.abs_pth, abspath(join(cwd, 'foo', fname)))
def test_pickle(self):
"""Test if pickler works correctly with FilePaths.
"""
t = Test()
t.f.set('t.vtk')
cwd = os.getcwd()
curdir = basename(cwd)
# Create a dummy file in the parent dir.
s = BytesIO()
# Spoof its location.
s.name = abspath(join(cwd, os.pardir, 't.mv2'))
# Dump into it
state_pickler.dump(t, s)
# Rewind the stream
s.seek(0)
# "Move" the file elsewhere
s.name = join(cwd, 'foo', 'test', 't.mv2')
state = state_pickler.load_state(s)
self.assertEqual(state.f.abs_pth,
join(cwd, 'foo', 'test', curdir, 't.vtk'))
# Create a dummy file in a subdir.
s = BytesIO()
# Spoof its location.
s.name = abspath(join(cwd, 'data', 't.mv2'))
# Dump into it.
state_pickler.dump(t, s)
# Rewind the stream
s.seek(0)
# "Move" the file elsewhere
s.name = join(cwd, 'foo', 'test', 't.mv2')
state = state_pickler.load_state(s)
self.assertEqual(state.f.abs_pth,
join(cwd, 'foo', 't.vtk'))
if __name__ == "__main__":
unittest.main()
|