import pytest
import util
import os
import dploy


@pytest.fixture(scope='module')
def source_a(request):
    tree = [
        {
            'source_a' : [
                {
                    'aaa': [
                        'aaa',
                        'bbb',
                        {
                            'ccc': [
                                'aaa',
                                'bbb',
                            ],
                        },
                    ],
                },
            ],
        },
    ]
    util.create_tree(tree)

    def source_a_teardown():
        util.remove_tree('source_a')
    request.addfinalizer(source_a_teardown)


@pytest.fixture(scope='module')
def source_b(request):
    tree = [
        {
            'source_b' : [
                {
                    'aaa': [
                        'ddd',
                        'eee',
                        {
                            'fff': [
                                'aaa',
                                'bbb',
                            ],
                        },
                    ],
                },
            ],
        },
    ]
    util.create_tree(tree)

    def source_b_teardown():
        util.remove_tree('source_b')
    request.addfinalizer(source_b_teardown)


@pytest.fixture(scope='module')
def dest(request):
    util.create_directory('dest')

    def dest_teardown():
        util.remove_tree('dest')
    request.addfinalizer(dest_teardown)


def test_stow_basic(source_a, dest):
    dploy.stow('source_a', 'dest')
    assert os.path.islink('dest/aaa')


def test_stow_twice(source_a, dest):
    dploy.stow('source_a', 'dest')
    dploy.stow('source_a', 'dest')
    assert os.path.islink('dest/aaa')


def test_stow_unfolding(source_a, source_b, dest):
    dploy.stow('source_a', 'dest')
    assert os.path.islink('dest/aaa')

    assert os.path.isfile('dest/aaa/aaa')
    assert os.path.isfile('dest/aaa/bbb')
    assert os.path.isdir('dest/aaa/ccc')

    dploy.stow('source_b', 'dest')
    assert os.path.isdir('dest/aaa')

    assert os.path.islink('dest/aaa/aaa')
    assert os.path.islink('dest/aaa/bbb')
    assert os.path.islink('dest/aaa/ccc')

    assert os.path.islink('dest/aaa/ddd')
    assert os.path.islink('dest/aaa/eee')
    assert os.path.islink('dest/aaa/fff')

@@ -91,32 +91,54 @@
 rce_a(request):%0A
+    name = 'source_a'%0A
     tree = %5B%0A   
@@ -156,26 +156,20 @@
         
-'source_a'
+name
  : %5B%0A   
@@ -629,26 +629,20 @@
 ve_tree(
-'source_a'
+name
 )%0A    re
@@ -732,24 +732,46 @@
 b(request):%0A
+    name = 'source_b'%0A
     tree = %5B
@@ -793,26 +793,20 @@
         
-'source_b'
+name
  : %5B%0A   
@@ -1266,26 +1266,20 @@
 ve_tree(
-'source_b'
+name
 )%0A    re
@@ -1365,24 +1365,42 @@
 t(request):%0A
+    name = 'dest'%0A
     util.cre
@@ -1413,22 +1413,20 @@
 rectory(
-'dest'
+name
 )%0A%0A    d
@@ -1470,22 +1470,20 @@
 ve_tree(
-'dest'
+name
 )%0A    re

use variable for repeated string