Skip to content

Commit

Permalink
test: add unit tests (#36)
Browse files Browse the repository at this point in the history
* test: add a unit test

* test: add tests
  • Loading branch information
suzuki-shunsuke authored Dec 23, 2024
1 parent 9dd4527 commit e652fcf
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 33 deletions.
7 changes: 1 addition & 6 deletions cmd/tfmv/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/logrus-error/logerr"
Expand Down Expand Up @@ -37,7 +34,5 @@ func core(logE *logrus.Entry) error {
},
LogE: logE,
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
return runner.Run(ctx) //nolint:wrapcheck
return runner.Run() //nolint:wrapcheck
}
49 changes: 24 additions & 25 deletions pkg/cli/runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -16,6 +15,28 @@ import (
"github.com/suzuki-shunsuke/tfmv/pkg/log"
)

const help = `tfmv - Rename Terraform resources, data sources, and modules and generate moved blocks.
https://github.com/suzuki-shunsuke/tfmv
Usage:
tfmv [<options>] [file ...]
One of --jsonnet (-j), --replace (-r), or --regexp must be specified.
Options:
--help, -h Show help
--version, -v Show sort-issue-template version
--replace, -r Replace strings in block names. The format is <old>/<new>. e.g. -/_
--jsonnet, -j Jsonnet file path
--regexp Replace strings in block names by regular expression. The format is <regular expression>/<new>. e.g. '\bfoo\b/bar'
--recursive, -R If this is set, tfmv finds files recursively
--include A regular expression to filter resources. Only resources that match the regular expression are renamed
--exclude A regular expression to filter resources. Only resources that don't match the regular expression are renamed
--dry-run Dry Run
--log-level Log level
--log-color Log color. "auto", "always", "never" are available
--moved, -m A file name where moved blocks are written. If this is "same", the file is same with renamed resources`

type Runner struct {
Stdin io.Reader
Stdout io.Writer
Expand All @@ -30,7 +51,7 @@ type LDFlags struct {
Date string
}

func (r *Runner) Run(ctx context.Context) error {
func (r *Runner) Run() error {
flg := &Flag{}
parseFlags(flg)
if flg.Version {
Expand Down Expand Up @@ -64,7 +85,7 @@ func (r *Runner) Run(ctx context.Context) error {

ctrl := &controller.Controller{}
ctrl.Init(afero.NewOsFs(), r.Stdout, r.Stderr)
return ctrl.Run(ctx, r.LogE, &controller.Input{ //nolint:wrapcheck
return ctrl.Run(r.LogE, &controller.Input{ //nolint:wrapcheck
File: flg.Jsonnet,
Dest: flg.Moved,
Recursive: flg.Recursive,
Expand Down Expand Up @@ -116,25 +137,3 @@ func parseFlags(f *Flag) {
flag.Parse()
f.Args = flag.Args()
}

const help = `tfmv - Rename Terraform resources, data sources, and modules and generate moved blocks.
https://github.com/suzuki-shunsuke/tfmv
Usage:
tfmv [<options>] [file ...]
One of --jsonnet (-j), --replace (-r), or --regexp must be specified.
Options:
--help, -h Show help
--version, -v Show sort-issue-template version
--replace, -r Replace strings in block names. The format is <old>/<new>. e.g. -/_
--jsonnet, -j Jsonnet file path
--regexp Replace strings in block names by regular expression. The format is <regular expression>/<new>. e.g. '\bfoo\b/bar'
--recursive, -R If this is set, tfmv finds files recursively
--include A regular expression to filter resources. Only resources that match the regular expression are renamed
--exclude A regular expression to filter resources. Only resources that don't match the regular expression are renamed
--dry-run Dry Run
--log-level Log level
--log-color Log color. "auto", "always", "never" are available
--moved, -m A file name where moved blocks are written. If this is "same", the file is same with renamed resources`
3 changes: 1 addition & 2 deletions pkg/controller/run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -13,7 +12,7 @@ import (
"github.com/suzuki-shunsuke/logrus-error/logerr"
)

func (c *Controller) Run(_ context.Context, logE *logrus.Entry, input *Input) error {
func (c *Controller) Run(logE *logrus.Entry, input *Input) error {
// read Jsonnet
renamer, err := NewRenamer(logE, c.fs, input)
if err != nil {
Expand Down
129 changes: 129 additions & 0 deletions pkg/controller/run_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package controller_test

import (
"bytes"
"io"
"path/filepath"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/tfmv/pkg/controller"
)

func TestController_Run(t *testing.T) { //nolint:funlen
t.Parallel()
tests := []struct {
name string
files map[string]string
stdout io.Writer
stderr io.Writer
input *controller.Input
isErr bool
}{
{
name: "no changed file",
files: map[string]string{
"main.tf": `resource "null_resource" "example_1" {}
`,
},
stdout: &bytes.Buffer{},
stderr: &bytes.Buffer{},
input: &controller.Input{
Args: []string{"main.tf"},
Replace: "-/_",
DryRun: true,
},
},
{
name: "replace",
files: map[string]string{
"testdata/main.tf": `resource "null_resource" "example-1" {}
`,
},
stdout: &bytes.Buffer{},
stderr: &bytes.Buffer{},
input: &controller.Input{
Args: []string{"testdata/main.tf"},
Replace: "-/_",
DryRun: true,
},
},
{
name: "regexp",
files: map[string]string{
"testdata/main.tf": `resource "null_resource" "example-1" {}
`,
},
stdout: &bytes.Buffer{},
stderr: &bytes.Buffer{},
input: &controller.Input{
Args: []string{"testdata/main.tf"},
Regexp: "^example-/test-",
DryRun: true,
},
},
{
name: "jsonnet",
files: map[string]string{
"testdata/main.tf": `resource "null_resource" "example-1" {}
`,
"main.jsonnet": `std.native("strings.Replace")(std.extVar('input').name, "-", "_", -1)[0]
`,
},
stdout: &bytes.Buffer{},
stderr: &bytes.Buffer{},
input: &controller.Input{
Args: []string{"testdata/main.tf"},
File: "main.jsonnet",
DryRun: true,
},
},
{
name: "no renamer",
files: map[string]string{
"testdata/main.tf": `resource "null_resource" "example-1" {}
`,
},
stdout: &bytes.Buffer{},
stderr: &bytes.Buffer{},
input: &controller.Input{},
isErr: true,
},
{
name: "no file is found",
files: map[string]string{},
stdout: &bytes.Buffer{},
stderr: &bytes.Buffer{},
input: &controller.Input{
Replace: "-/_",
},
},
}
logE := logrus.NewEntry(logrus.New())
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
fs := afero.NewMemMapFs()
for path, content := range tt.files {
if err := fs.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := afero.WriteFile(fs, path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
ctrl := &controller.Controller{}
ctrl.Init(fs, tt.stdout, tt.stderr)
if err := ctrl.Run(logE, tt.input); err != nil {
if tt.isErr {
return
}
t.Fatal(err)
}
if tt.isErr {
t.Fatal("error is expected")
}
})
}
}
1 change: 1 addition & 0 deletions pkg/controller/testdata/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resource "null_resource" "example-1" {}

0 comments on commit e652fcf

Please sign in to comment.