From 0a6b254641e2960c13bd5a0819ffc2d771c49ccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Sj=C3=B6gren?= Date: Tue, 29 Mar 2022 14:43:28 +0200 Subject: [PATCH 1/2] add `vagrant validate` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Sjögren --- tests/test_vagrant.py | 7 +++++++ vagrant/__init__.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/tests/test_vagrant.py b/tests/test_vagrant.py index 45c65d4..9d8de81 100644 --- a/tests/test_vagrant.py +++ b/tests/test_vagrant.py @@ -283,6 +283,13 @@ def test_vm_lifecycle(vm_dir): assert v.NOT_CREATED == v.status()[0].state +def test_valid_config(vm_dir): + v = vagrant.Vagrant(vm_dir) + v.up() + output = v.validate() + assert output.strip() == "Vagrantfile validated successfully." + + def test_vm_config(vm_dir): """ Test methods retrieving ssh config settings, like user, hostname, and port. diff --git a/vagrant/__init__.py b/vagrant/__init__.py index 6eaf437..dae0a93 100644 --- a/vagrant/__init__.py +++ b/vagrant/__init__.py @@ -876,6 +876,13 @@ def plugin_list(self): output = self._run_vagrant_command(["plugin", "list", "--machine-readable"]) return self._parse_plugin_list(output) + def validate(self): + """ + This command validates present Vagrantfile. + """ + output = self._run_vagrant_command(["validate"]) + return output + def _parse_plugin_list(self, output): """ Remove Vagrant from the equation for unit testing. From c0b8a8116a6d4a107b6af9ec39b7adaf5957f99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Sj=C3=B6gren?= Date: Wed, 30 Mar 2022 09:43:02 +0200 Subject: [PATCH 2/2] use subprocess and assert the return code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Sjögren --- tests/test_vagrant.py | 5 +++-- vagrant/__init__.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/test_vagrant.py b/tests/test_vagrant.py index 9d8de81..cf091e8 100644 --- a/tests/test_vagrant.py +++ b/tests/test_vagrant.py @@ -286,8 +286,9 @@ def test_vm_lifecycle(vm_dir): def test_valid_config(vm_dir): v = vagrant.Vagrant(vm_dir) v.up() - output = v.validate() - assert output.strip() == "Vagrantfile validated successfully." + validation = v.validate(vm_dir) + + assert validation.returncode == 0 def test_vm_config(vm_dir): diff --git a/vagrant/__init__.py b/vagrant/__init__.py index dae0a93..83fd21b 100644 --- a/vagrant/__init__.py +++ b/vagrant/__init__.py @@ -876,12 +876,19 @@ def plugin_list(self): output = self._run_vagrant_command(["plugin", "list", "--machine-readable"]) return self._parse_plugin_list(output) - def validate(self): + def validate(self, directory): """ This command validates present Vagrantfile. """ - output = self._run_vagrant_command(["validate"]) - return output + validate = subprocess.run( + ["vagrant", "validate"], + cwd=directory, + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + return validate def _parse_plugin_list(self, output): """