Skip to content

Commit

Permalink
Provide a public static method Tree.init()
Browse files Browse the repository at this point in the history
  • Loading branch information
psss committed Oct 10, 2019
1 parent c15ad90 commit 6d3f6e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 16 additions & 0 deletions fmf/base.py
Expand Up @@ -158,6 +158,22 @@ def _merge_minus(self, data, key, value):
"MergeError: Key '{0}' in {1} (wrong type).".format(
key, self.name))

@staticmethod
def init(path):
""" Create metadata tree root under given path """
root = os.path.abspath(os.path.join(path, ".fmf"))
if os.path.exists(root):
raise utils.FileError("{0} '{1}' already exists.".format(
"Directory" if os.path.isdir(root) else "File", root))
try:
os.makedirs(root)
with open(os.path.join(root, "version"), "w") as version:
version.write("{0}\n".format(utils.VERSION))
except OSError as error:
raise utils.FileError("Failed to create '{}': {}.".format(
root, error))
return root

def merge(self, parent=None):
""" Merge parent data """
# Check parent, append source files
Expand Down
8 changes: 1 addition & 7 deletions fmf/cli.py
Expand Up @@ -142,13 +142,7 @@ def command_init(self):
self.options = self.parser.parse_args(self.arguments[2:])
# For each path create an .fmf directory and version file
for path in self.options.paths or ["."]:
root = os.path.abspath(os.path.join(path, ".fmf"))
if os.path.exists(root):
raise utils.FileError("{0} '{1}' already exists.".format(
"Directory" if os.path.isdir(root) else "File", root))
os.makedirs(root)
with open(os.path.join(root, "version"), "w") as version:
version.write("{0}\n".format(utils.VERSION))
root = fmf.Tree.init(path)
print("Metadata tree '{0}' successfully initialized.".format(root))

def show(self, brief=False):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_cli.py
Expand Up @@ -116,6 +116,13 @@ def test_init(self):
version_path = os.path.join(path, ".fmf", "version")
with open(version_path) as version:
assert "1" in version.read()
# Permission denied
secret_path = os.path.join(path, 'denied')
os.makedirs(secret_path)
os.chmod(secret_path, 0o666)
with pytest.raises(utils.FileError):
fmf.cli.main('fmf init --path {}'.format(secret_path), path)
os.chmod(secret_path, 0o777)
# Invalid version
with open(version_path, "w") as version:
version.write("bad")
Expand Down

0 comments on commit 6d3f6e9

Please sign in to comment.