link_tree: ported to pytest (#4008)

This commit is contained in:
Massimiliano Culpo 2017-04-27 00:36:35 +02:00 committed by Todd Gamblin
parent 5422ac1558
commit 0403a08509

View File

@ -23,121 +23,130 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
############################################################################## ##############################################################################
import os import os
import unittest
import pytest
from llnl.util.filesystem import * from llnl.util.filesystem import *
from llnl.util.link_tree import LinkTree from llnl.util.link_tree import LinkTree
from spack.stage import Stage from spack.stage import Stage
class LinkTreeTest(unittest.TestCase): @pytest.fixture()
"""Tests Spack's LinkTree class.""" def stage():
"""Creates a stage with the directory structure for the tests."""
s = Stage('link-tree-test')
s.create()
def setUp(self): with working_dir(s.path):
self.stage = Stage('link-tree-test') touchp('source/1')
self.stage.create() touchp('source/a/b/2')
touchp('source/a/b/3')
touchp('source/c/4')
touchp('source/c/d/5')
touchp('source/c/d/6')
touchp('source/c/d/e/7')
with working_dir(self.stage.path): yield s
touchp('source/1')
touchp('source/a/b/2')
touchp('source/a/b/3')
touchp('source/c/4')
touchp('source/c/d/5')
touchp('source/c/d/6')
touchp('source/c/d/e/7')
source_path = os.path.join(self.stage.path, 'source') s.destroy()
self.link_tree = LinkTree(source_path)
def tearDown(self):
self.stage.destroy()
def check_file_link(self, filename): @pytest.fixture()
self.assertTrue(os.path.isfile(filename)) def link_tree(stage):
self.assertTrue(os.path.islink(filename)) """Return a properly initialized LinkTree instance."""
source_path = os.path.join(stage.path, 'source')
return LinkTree(source_path)
def check_dir(self, filename):
self.assertTrue(os.path.isdir(filename))
def test_merge_to_new_directory(self): def check_file_link(filename):
with working_dir(self.stage.path): assert os.path.isfile(filename)
self.link_tree.merge('dest') assert os.path.islink(filename)
self.check_file_link('dest/1')
self.check_file_link('dest/a/b/2')
self.check_file_link('dest/a/b/3')
self.check_file_link('dest/c/4')
self.check_file_link('dest/c/d/5')
self.check_file_link('dest/c/d/6')
self.check_file_link('dest/c/d/e/7')
self.link_tree.unmerge('dest') def check_dir(filename):
assert os.path.isdir(filename)
self.assertFalse(os.path.exists('dest'))
def test_merge_to_existing_directory(self): def test_merge_to_new_directory(stage, link_tree):
with working_dir(self.stage.path): with working_dir(stage.path):
link_tree.merge('dest')
touchp('dest/x') check_file_link('dest/1')
touchp('dest/a/b/y') check_file_link('dest/a/b/2')
check_file_link('dest/a/b/3')
check_file_link('dest/c/4')
check_file_link('dest/c/d/5')
check_file_link('dest/c/d/6')
check_file_link('dest/c/d/e/7')
self.link_tree.merge('dest') link_tree.unmerge('dest')
self.check_file_link('dest/1') assert not os.path.exists('dest')
self.check_file_link('dest/a/b/2')
self.check_file_link('dest/a/b/3')
self.check_file_link('dest/c/4')
self.check_file_link('dest/c/d/5')
self.check_file_link('dest/c/d/6')
self.check_file_link('dest/c/d/e/7')
self.assertTrue(os.path.isfile('dest/x'))
self.assertTrue(os.path.isfile('dest/a/b/y'))
self.link_tree.unmerge('dest') def test_merge_to_existing_directory(stage, link_tree):
with working_dir(stage.path):
self.assertTrue(os.path.isfile('dest/x')) touchp('dest/x')
self.assertTrue(os.path.isfile('dest/a/b/y')) touchp('dest/a/b/y')
self.assertFalse(os.path.isfile('dest/1')) link_tree.merge('dest')
self.assertFalse(os.path.isfile('dest/a/b/2'))
self.assertFalse(os.path.isfile('dest/a/b/3'))
self.assertFalse(os.path.isfile('dest/c/4'))
self.assertFalse(os.path.isfile('dest/c/d/5'))
self.assertFalse(os.path.isfile('dest/c/d/6'))
self.assertFalse(os.path.isfile('dest/c/d/e/7'))
def test_merge_with_empty_directories(self): check_file_link('dest/1')
with working_dir(self.stage.path): check_file_link('dest/a/b/2')
mkdirp('dest/f/g') check_file_link('dest/a/b/3')
mkdirp('dest/a/b/h') check_file_link('dest/c/4')
check_file_link('dest/c/d/5')
check_file_link('dest/c/d/6')
check_file_link('dest/c/d/e/7')
self.link_tree.merge('dest') assert os.path.isfile('dest/x')
self.link_tree.unmerge('dest') assert os.path.isfile('dest/a/b/y')
self.assertFalse(os.path.exists('dest/1')) link_tree.unmerge('dest')
self.assertFalse(os.path.exists('dest/a/b/2'))
self.assertFalse(os.path.exists('dest/a/b/3'))
self.assertFalse(os.path.exists('dest/c/4'))
self.assertFalse(os.path.exists('dest/c/d/5'))
self.assertFalse(os.path.exists('dest/c/d/6'))
self.assertFalse(os.path.exists('dest/c/d/e/7'))
self.assertTrue(os.path.isdir('dest/a/b/h')) assert os.path.isfile('dest/x')
self.assertTrue(os.path.isdir('dest/f/g')) assert os.path.isfile('dest/a/b/y')
def test_ignore(self): assert not os.path.isfile('dest/1')
with working_dir(self.stage.path): assert not os.path.isfile('dest/a/b/2')
touchp('source/.spec') assert not os.path.isfile('dest/a/b/3')
touchp('dest/.spec') assert not os.path.isfile('dest/c/4')
assert not os.path.isfile('dest/c/d/5')
assert not os.path.isfile('dest/c/d/6')
assert not os.path.isfile('dest/c/d/e/7')
self.link_tree.merge('dest', ignore=lambda x: x == '.spec')
self.link_tree.unmerge('dest', ignore=lambda x: x == '.spec')
self.assertFalse(os.path.exists('dest/1')) def test_merge_with_empty_directories(stage, link_tree):
self.assertFalse(os.path.exists('dest/a')) with working_dir(stage.path):
self.assertFalse(os.path.exists('dest/c')) mkdirp('dest/f/g')
mkdirp('dest/a/b/h')
self.assertTrue(os.path.isfile('source/.spec')) link_tree.merge('dest')
self.assertTrue(os.path.isfile('dest/.spec')) link_tree.unmerge('dest')
assert not os.path.exists('dest/1')
assert not os.path.exists('dest/a/b/2')
assert not os.path.exists('dest/a/b/3')
assert not os.path.exists('dest/c/4')
assert not os.path.exists('dest/c/d/5')
assert not os.path.exists('dest/c/d/6')
assert not os.path.exists('dest/c/d/e/7')
assert os.path.isdir('dest/a/b/h')
assert os.path.isdir('dest/f/g')
def test_ignore(stage, link_tree):
with working_dir(stage.path):
touchp('source/.spec')
touchp('dest/.spec')
link_tree.merge('dest', ignore=lambda x: x == '.spec')
link_tree.unmerge('dest', ignore=lambda x: x == '.spec')
assert not os.path.exists('dest/1')
assert not os.path.exists('dest/a')
assert not os.path.exists('dest/c')
assert os.path.isfile('source/.spec')
assert os.path.isfile('dest/.spec')