-
Notifications
You must be signed in to change notification settings - Fork 2
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
added unit tests for the terraform
package
#461
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
package terraform | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hazelops/ize/internal/config" | ||
"github.com/hazelops/ize/pkg/terminal" | ||
"io" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestInstall(t *testing.T) { | ||
type args struct { | ||
tfversion string | ||
mirrorURL string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{name: "success", args: args{ | ||
tfversion: "1.1.9", | ||
mirrorURL: defaultMirror, | ||
}, wantErr: false}, | ||
{name: "success exist", args: args{ | ||
psihachina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tfversion: "1.1.9", | ||
mirrorURL: defaultMirror, | ||
}, wantErr: false}, | ||
{name: "invalid mirror", args: args{ | ||
tfversion: "1.2.9", | ||
mirrorURL: "invalid.com/mirror", | ||
}, wantErr: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := Install(tt.args.tfversion, tt.args.mirrorURL); (err != nil) != tt.wantErr { | ||
t.Errorf("Install() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_installVersion(t *testing.T) { | ||
mirror := defaultMirror | ||
hd := home() | ||
|
||
type args struct { | ||
version string | ||
mirrorURL *string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{name: "success", args: args{ | ||
version: "1.1.3", | ||
mirrorURL: &mirror, | ||
}, want: fmt.Sprintf("%s/%s", hd, ".ize/versions/terraform/terraform_1.1.3"), wantErr: false}, | ||
{name: "success exist", args: args{ | ||
version: "1.1.3", | ||
mirrorURL: &mirror, | ||
}, want: fmt.Sprintf("%s/%s", hd, ".ize/versions/terraform/terraform_1.1.3"), wantErr: false}, | ||
{name: "invalid tf version", args: args{ | ||
version: "1.1.X", | ||
mirrorURL: &mirror, | ||
}, wantErr: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := installVersion(tt.args.version, tt.args.mirrorURL) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("installVersion() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("installVersion() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_local_Prepare(t *testing.T) { | ||
type fields struct { | ||
version string | ||
command []string | ||
env []string | ||
output io.Writer | ||
tfpath string | ||
project *config.Project | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
}{ | ||
{name: "success", fields: fields{ | ||
version: "1.1.3", | ||
command: []string{"version"}, | ||
env: nil, | ||
output: os.Stdout, | ||
tfpath: "", | ||
project: &config.Project{}, | ||
}, wantErr: false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := &local{ | ||
version: tt.fields.version, | ||
command: tt.fields.command, | ||
env: tt.fields.env, | ||
output: tt.fields.output, | ||
tfpath: tt.fields.tfpath, | ||
project: tt.fields.project, | ||
} | ||
if err := l.Prepare(); (err != nil) != tt.wantErr { | ||
t.Errorf("Prepare() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_local_Run(t *testing.T) { | ||
mirror := defaultMirror | ||
version, err := installVersion("1.1.3", &mirror) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
type fields struct { | ||
version string | ||
command []string | ||
env []string | ||
output io.Writer | ||
tfpath string | ||
project *config.Project | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
}{ | ||
{name: "success", fields: fields{ | ||
version: "1.1.3", | ||
command: []string{"version"}, | ||
env: nil, | ||
output: os.Stdout, | ||
tfpath: version, | ||
project: &config.Project{}, | ||
}, wantErr: false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := &local{ | ||
version: tt.fields.version, | ||
command: tt.fields.command, | ||
env: tt.fields.env, | ||
output: tt.fields.output, | ||
tfpath: tt.fields.tfpath, | ||
project: tt.fields.project, | ||
} | ||
if err := l.Run(); (err != nil) != tt.wantErr { | ||
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_local_RunUI(t *testing.T) { | ||
mirror := defaultMirror | ||
version, err := installVersion("1.1.3", &mirror) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
type fields struct { | ||
version string | ||
command []string | ||
env []string | ||
output io.Writer | ||
tfpath string | ||
project *config.Project | ||
} | ||
type args struct { | ||
ui terminal.UI | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "success", | ||
fields: fields{ | ||
version: "1.1.3", | ||
command: []string{"version"}, | ||
env: nil, | ||
output: os.Stdout, | ||
tfpath: version, | ||
project: &config.Project{}, | ||
}, | ||
args: args{ui: terminal.ConsoleUI(context.TODO(), true)}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := &local{ | ||
version: tt.fields.version, | ||
command: tt.fields.command, | ||
env: tt.fields.env, | ||
output: tt.fields.output, | ||
tfpath: tt.fields.tfpath, | ||
project: tt.fields.project, | ||
} | ||
if err := l.RunUI(tt.args.ui); (err != nil) != tt.wantErr { | ||
t.Errorf("RunUI() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func home() string { | ||
h, _ := os.UserHomeDir() | ||
return h | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include the name of the package in the test name (here and below)? Won't it be unclear which package this is related to when the whole test suite is executed?
TestTerraform_Install
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AutomationD the test of the terraform package and the name of the function is
Install
, notTerraformIntall
, so everything is fine