Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#57 Adding support for terraform validate command. #58

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/input/docs/usage/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,17 @@ Task("Destroy")
};
TerraformDestroy(settings);
});
```


## TerraformValidate

```csharp
#addin Cake.Terraform

Task("Validate")
.Does(() =>
{
TerraformValidate();
});
```
97 changes: 97 additions & 0 deletions src/Cake.Terraform.Tests/TerraformValidateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Collections.Generic;
using Cake.Core;
using Cake.Terraform.Validate;
using Cake.Testing;
using Xunit;

namespace Cake.Terraform.Tests
{
public class TerraformValidateTests
{
class Fixture : TerraformFixture<TerraformValidateSettings>
{
public Fixture(PlatformFamily platformFamily = PlatformFamily.Windows) : base(platformFamily) { }

protected override void RunTool()
{
ProcessRunner.Process.SetStandardOutput(new List<string> { "default" });

var tool = new TerraformValidateRunner(FileSystem, Environment, ProcessRunner, Tools);

tool.Run(Settings);
}
}

public class TheExecutable
{
[Fact]
public void Should_throw_if_terraform_runner_was_not_found()
{
var fixture = new Fixture();
fixture.GivenDefaultToolDoNotExist();

var result = Record.Exception(() => fixture.Run());

Assert.IsType<CakeException>(result);
Assert.Equal("Terraform: Could not locate executable.", result.Message);
}

[Theory]
[InlineData("/bin/tools/terraform/terraform.exe", "/bin/tools/terraform/terraform.exe")]
[InlineData("/bin/tools/terraform/terraform", "/bin/tools/terraform/terraform")]
public void Should_use_terraform_from_tool_path_if_provided(string toolPath, string expected)
{
var fixture = new Fixture() { Settings = { ToolPath = toolPath } };
fixture.GivenSettingsToolPathExist();

var result = fixture.Run();

Assert.Equal(expected, result.Path.FullPath);
}

[Fact]
public void Should_find_terraform_if_tool_path_not_provided()
{
var fixture = new Fixture();

var result = fixture.Run();

Assert.Equal("/Working/tools/terraform.exe", result.Path.FullPath);
}

[Fact]
public void Should_throw_if_process_has_a_non_zero_exit_code()
{
var fixture = new Fixture();
fixture.GivenProcessExitsWithCode(1);

var result = Record.Exception(() => fixture.Run());

Assert.IsType<CakeException>(result);
Assert.Equal("Terraform: Process returned an error (exit code 1).", result.Message);
}

[Fact]
public void Should_find_linux_executable()
{
var fixture = new Fixture(PlatformFamily.Linux);
fixture.Environment.Platform.Family = PlatformFamily.Linux;


var result = fixture.Run();

Assert.Equal("/Working/tools/terraform", result.Path.FullPath);
}

[Fact]
public void Should_set_workspace_and_list_parameter()
{
var fixture = new Fixture();

var result = fixture.Run();

Assert.Contains("validate", result.Args);
}
}
}
}
14 changes: 14 additions & 0 deletions src/Cake.Terraform/TerraformAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Cake.Terraform.Plan;
using Cake.Terraform.Refresh;
using Cake.Terraform.Show;
using Cake.Terraform.Validate;

namespace Cake.Terraform
{
Expand Down Expand Up @@ -149,6 +150,19 @@ public static void TerraformRefresh(this ICakeContext context, TerraformRefreshS
runner.Run(settings);
}

[CakeMethodAlias]
public static void TerraformValidate(this ICakeContext context)
{
TerraformValidate(context, new TerraformValidateSettings());
}

[CakeMethodAlias]
public static void TerraformValidate(this ICakeContext context, TerraformValidateSettings settings)
{
var runner = new TerraformValidateRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
runner.Run(settings);
}

[CakeMethodAlias]
public static string TerraformOutput(this ICakeContext context, TerraformOutputSettings settings)
{
Expand Down
32 changes: 32 additions & 0 deletions src/Cake.Terraform/Validate/TerraformValidateRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;
using Cake.Terraform.Init;

namespace Cake.Terraform.Validate
{
public class TerraformValidateRunner : TerraformRunner<TerraformValidateSettings>
{
public TerraformValidateRunner(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools)
: base(fileSystem, environment, processRunner, tools)
{
}

public void Run(TerraformValidateSettings settings)
{
var builder = new ProcessArgumentBuilder().Append("validate");


Run(settings, builder);
}
}

public class TerraformValidateSettings : TerraformSettings
{
}
}
Loading