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. """, )
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. """, )
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")
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. """)
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. """)
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. """)
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. """, )
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)
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. """)
def test_anatomy_file(datadir): # Prepare f = AnatomyFile('gitignore', """ a b """) # Execute f.apply(datadir, variables={}) # Check assert_file_contents(datadir + '/gitignore', """ a b """)
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. """, )
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 """)
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)
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)
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')
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. """)