Beispiel #1
0
def test_anatomy_feature_from_yaml(datadir):
    AnatomyFeatureRegistry.clear()
    AnatomyFeatureRegistry.register_from_text("""
            anatomy-features:
              - name: CREATEFILE
                variables:
                  code: BRAVO
                create-file:
                  filename: filename.txt
                  contents: |
                     # This file is generated by zops.anatomy.
        """)
    assert AnatomyFeatureRegistry.tree() == [("CREATEFILE", "filename.txt",
                                              "filename.txt")]

    feature = AnatomyFeatureRegistry.get("CREATEFILE")
    assert feature.filename == "filename.txt"

    tree = _play_feature(feature, datadir)
    assert tree._AnatomyTree__variables == {
        "CREATEFILE": {
            "code": "BRAVO"
        },
    }

    assert_file_contents(
        datadir + "/filename.txt",
        """
            # This file is generated by zops.anatomy.
        """,
    )
Beispiel #2
0
def test_anatomy_feature_with_variable(datadir):
    feature = AnatomyFeature("CREATEFILE", variables={"code": "ALPHA"})
    feature.create_file(
        "filename.txt",
        "# This file UNKNOWN from feature {{ CREATEFILE.code }}.",
    )
    AnatomyFeatureRegistry.clear()
    assert AnatomyFeatureRegistry.tree() == []
    AnatomyFeatureRegistry.register("CREATEFILE", feature)
    assert AnatomyFeatureRegistry.tree() == [("CREATEFILE", "filename.txt",
                                              "filename.txt")]

    # Apply Feature
    tree = AnatomyTree()
    assert tree._AnatomyTree__variables == {}
    feature.apply(tree)
    assert tree._AnatomyTree__variables == {
        "CREATEFILE": {
            "code": "ALPHA"
        },
    }
    tree.apply(datadir)

    assert_file_contents(
        datadir + "/filename.txt",
        """
            # This file UNKNOWN from feature ALPHA.
        """,
    )
Beispiel #3
0
def test_anatomy_feature(datadir):
    feature = AnatomyFeature("createfile")
    feature.create_file("filename.txt",
                        "# This file is generated by zops.anatomy.")

    _play_feature(feature, datadir)

    assert_file_contents(
        datadir + "/filename.txt",
        """
            # This file is generated by zops.anatomy.
        """,
    )

    # Now, create a link to filename.txt
    feature = AnatomyFeature("createlink")
    feature.create_link("symlink.txt", "filename.txt")

    _play_feature(feature, datadir)

    assert_file_contents(
        datadir + "/symlink.txt",
        """
            # This file is generated by zops.anatomy.
        """,
    )
    assert os.path.islink(datadir + "/symlink.txt")
    assert not os.path.islink(datadir + "/filename.txt")
Beispiel #4
0
def test_anatomy_feature_from_yaml(datadir):
    AnatomyFeatureRegistry.clear()
    AnatomyFeatureRegistry.register_from_text("""
            anatomy-features:
              - name: CREATEFILE
                variables:
                  code: BRAVO
                create-file:
                  filename: filename.txt
                  contents: |
                     # This file is generated by zops.anatomy.
        """)
    assert AnatomyFeatureRegistry.tree() == [('CREATEFILE', 'filename.txt',
                                              'filename.txt')]

    feature = AnatomyFeatureRegistry.get('CREATEFILE')
    assert feature.filename == 'filename.txt'

    tree = _play_feature(feature, datadir)
    assert tree._AnatomyTree__variables == {
        'CREATEFILE': {
            'code': 'BRAVO'
        },
    }

    assert_file_contents(
        datadir + '/filename.txt', """
            # This file is generated by zops.anatomy.
        """)
Beispiel #5
0
def test_anatomy_feature_with_variable(datadir):
    feature = AnatomyFeature('CREATEFILE', variables={'code': 'ALPHA'})
    feature.create_file(
        'filename.txt',
        '# This file UNKNOWN from feature {{ CREATEFILE.code }}.',
    )
    AnatomyFeatureRegistry.clear()
    assert AnatomyFeatureRegistry.tree() == []
    AnatomyFeatureRegistry.register('CREATEFILE', feature)
    assert AnatomyFeatureRegistry.tree() == [('CREATEFILE', 'filename.txt',
                                              'filename.txt')]

    # Apply Feature
    tree = AnatomyTree()
    assert tree._AnatomyTree__variables == {}
    feature.apply(tree)
    assert tree._AnatomyTree__variables == {
        'CREATEFILE': {
            'code': 'ALPHA'
        },
    }
    tree.apply(datadir)

    assert_file_contents(
        datadir + '/filename.txt', """
            # This file UNKNOWN from feature ALPHA.
        """)
Beispiel #6
0
def test_anatomy_file_replace_filename_with_variable(datadir):
    f = AnatomyFile("alpha.txt", 'This is alpha.')
    f.apply(datadir, variables={}, filename='zulu.txt')
    assert not os.path.isfile(datadir + '/alpha.txt')
    assert_file_contents(datadir + '/zulu.txt', """
            This is alpha.
        """)
Beispiel #7
0
def test_anatomy_file_with_filenames_using_variables(datadir):
    f = AnatomyFile("{{filename}}", "This is alpha.")
    f.apply(datadir, variables={"filename": "alpha.txt"})
    assert_file_contents(
        datadir + "/alpha.txt",
        """
            This is alpha.
        """,
    )
Beispiel #8
0
        def check(self, seed):
            if isinstance(seed, str):
                contents = _to_contents(seed)
            elif isinstance(seed, dict):
                contents = seed
            else:
                raise TypeError(seed.__class__)
            AnatomyFeatureRegistry.clear()
            AnatomyFeatureRegistry.register_from_contents(contents)
            playbook = AnatomyPlaybook.from_contents(contents)
            target_dir = datadir + '/target'
            playbook.apply(target_dir)

            for i_filename, i_expected in contents['target'].items():
                assert_file_contents(target_dir.join(i_filename), i_expected)
Beispiel #9
0
def test_anatomy_tree_with_variables(datadir):

    # Prepare
    tree = AnatomyTree()
    tree.create_file('alpha.txt', 'This is {{ name }}.')

    # Without defined variables
    with pytest.raises(RuntimeError):
        tree.apply(datadir)

    # With defined variables
    tree.add_variables({'name': 'ALPHA'}, left_join=False)
    tree.apply(datadir)
    assert_file_contents(datadir + '/alpha.txt', """
            This is ALPHA.
        """)
Beispiel #10
0
def test_anatomy_file(datadir):

    # Prepare
    f = AnatomyFile('gitignore', """
            a
            b
        """)

    # Execute
    f.apply(datadir, variables={})

    # Check
    assert_file_contents(datadir + '/gitignore', """
            a
            b
        """)
Beispiel #11
0
def test_anatomy_tree_with_variables(datadir):

    # Prepare
    tree = AnatomyTree()
    tree.create_file("alpha.txt", "This is {{ name }}.")

    # Without defined variables
    with pytest.raises(RuntimeError):
        tree.apply(datadir)

    # With defined variables
    tree.add_variables({"name": "ALPHA"}, left_join=False)
    tree.apply(datadir)
    assert_file_contents(
        datadir + "/alpha.txt",
        """
            This is ALPHA.
        """,
    )
Beispiel #12
0
def test_anatomy_tree(datadir):

    # Prepare
    tree = AnatomyTree()
    tree.create_file(
        '.gitignore',
        'line 1\n{% for i in gitignore.blocks %}{{ i }}{% endfor %}\n')
    tree.add_variables(dict(gitignore=dict(blocks=['line 2'])),
                       left_join=False)

    # Execute
    tree.apply(datadir)

    # Check
    assert_file_contents(
        datadir + '/.gitignore', """
            line 1
            line 2
        """)
Beispiel #13
0
def test_anatomy_file_executable(datadir):

    # Prepare
    f = AnatomyFile('gitignore',
                    """
            a
            b
        """,
                    executable=True)

    # Execute
    f.apply(datadir, variables={})

    # Check
    assert_file_contents(datadir + '/gitignore', """
            a
            b
        """)
    assert os.access(datadir + '/gitignore', os.X_OK)
Beispiel #14
0
        def check(self, seed):
            if isinstance(seed, str):
                contents = _to_contents(seed)
            elif isinstance(seed, dict):
                contents = seed
            else:
                raise TypeError(seed.__class__)
            AnatomyFeatureRegistry.clear()
            AnatomyFeatureRegistry.register_from_contents(contents)
            playbook = AnatomyPlaybook.from_contents(contents)
            target_dir = datadir + "/target"
            playbook.apply(target_dir)

            for i_filename, i_expected in contents["target"].items():
                if i_expected.strip() == "!":
                    assert not os.path.isfile(target_dir.join(
                        i_filename)), "File exists: {}".format(i_filename)
                else:
                    assert_file_contents(target_dir.join(i_filename),
                                         i_expected)
Beispiel #15
0
def test_anatomy_feature(datadir):
    feature = AnatomyFeature('createfile')
    feature.create_file('filename.txt',
                        '# This file is generated by zops.anatomy.')

    _play_feature(feature, datadir)

    assert_file_contents(
        datadir + '/filename.txt', """
            # This file is generated by zops.anatomy.
        """)

    # Now, create a link to filename.txt
    feature = AnatomyFeature('createlink')
    feature.create_link('symlink.txt', 'filename.txt')

    _play_feature(feature, datadir)

    assert_file_contents(
        datadir + '/symlink.txt', """
            # This file is generated by zops.anatomy.
        """)
    assert os.path.islink(datadir + '/symlink.txt')
    assert not os.path.islink(datadir + '/filename.txt')
Beispiel #16
0
def test_anatomy_file_with_filenames_using_variables(datadir):
    f = AnatomyFile("{{filename}}", 'This is alpha.')
    f.apply(datadir, variables={'filename': 'alpha.txt'})
    assert_file_contents(datadir + '/alpha.txt', """
            This is alpha.
        """)