Skip to content

Commit

Permalink
Do not override 1build.yaml if already exists in --init command (#104)
Browse files Browse the repository at this point in the history
* Do not override file if exists for init command
Issue: #103

* Add --init support in readme
Issue: #103
  • Loading branch information
gopinath-langote authored Jun 21, 2019
1 parent d64ca02 commit 82b9517
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ optional arguments:
-h, --help Print this help message
-l, --list Show all available commands - from `1build.yaml` file
-v, --version Show version of 1build and exit
-i, --init Create default `1build.yaml` configuration file
```

## Contributing
Expand Down
10 changes: 9 additions & 1 deletion onebuild/init_command.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from onebuild.command import Command
from onebuild.file_writer import write

Expand All @@ -12,11 +14,17 @@ class InitCommand(Command):

def execute(self, arg_parser, arguments, build_file_name, command_name):
if len(arguments) < 2:
raise ValueError(__project_name_not_found_error_message__())
print(__project_name_not_found_error_message__())
elif os.path.isfile("1build.yaml"):
print(__file_already_exists_message__())
else:
write("1build.yaml", "w", default_yaml_file(arguments[1]))


def __project_name_not_found_error_message__():
return "The 'project name' parameter is missing with --init" \
"\n\nusage: 1build --init project_name"


def __file_already_exists_message__():
return "1build.yaml configuration file already exists."
29 changes: 27 additions & 2 deletions tests/test_init_command.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/usr/bin/env python

import sys
from io import StringIO
from unittest.mock import patch, MagicMock

from onebuild.main import run


def test_create_default_yaml_file():
@patch('os.path.isfile')
def test_create_default_yaml_file(mock_isfile):
mock_file_writer = MagicMock()

mock_isfile.return_value = False

with patch("onebuild.init_command.write",
mock_file_writer,
create=True):
Expand All @@ -24,3 +28,24 @@ def test_error_message_if_file_name_is_not_provided_with_init(capsys):
expected_message = "The 'project name' parameter is missing with --init" \
"\n\nusage: 1build --init project_name"
assert expected_message in captured.out


@patch('os.path.isfile')
def test_error_message_if_file_already_exists(mock_isfile):
mock_file_writer = MagicMock()

mock_isfile.return_value = True

captured_output = StringIO()
sys.stdout = captured_output

with patch("onebuild.init_command.write",
mock_file_writer,
create=True):
run("", ['--init', 'some project'])

sys.stdout = sys.__stdout__
assert "1build.yaml configuration file already exists." \
in captured_output.getvalue()

mock_file_writer.assert_not_called()

0 comments on commit 82b9517

Please sign in to comment.