github.com/wawandco/oxplugins@v0.7.11/tools/tasks/generator_test.go (about)

     1  package tasks
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"path/filepath"
    10  	"regexp"
    11  	"testing"
    12  )
    13  
    14  func Test_ActionGenerator(t *testing.T) {
    15  	g := Generator{}
    16  
    17  	t.Run("generate task", func(t *testing.T) {
    18  		dir := t.TempDir()
    19  		modelsPath := filepath.Join(dir, "app", "tasks")
    20  		if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil {
    21  			t.Errorf("creating templates folder should not be error, but got %v", err)
    22  		}
    23  
    24  		if err := g.Generate(context.Background(), dir, []string{"generate", "task", "simple"}); err != nil {
    25  			t.Errorf("should not be error, but got %v", err)
    26  		}
    27  
    28  		// Validating Files existence
    29  		if !g.exists(filepath.Join(modelsPath, "simple.go")) {
    30  			t.Error("'simple.go' file does not exists on the path")
    31  		}
    32  	})
    33  	t.Run("generate task and checking the content", func(t *testing.T) {
    34  		dir := t.TempDir()
    35  		modelsPath := filepath.Join(dir, "app", "tasks")
    36  		if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil {
    37  			t.Errorf("creating templates folder should not be error, but got %v", err)
    38  		}
    39  
    40  		if err := g.Generate(context.Background(), dir, []string{"generate", "task", "simple"}); err != nil {
    41  			t.Errorf("should not be error, but got %v", err)
    42  		}
    43  
    44  		// Validating Files existence
    45  		if !g.exists(filepath.Join(modelsPath, "simple.go")) {
    46  			t.Error("'simple.go' file does not exists on the path")
    47  		}
    48  
    49  		content, err := ioutil.ReadFile(filepath.Join(modelsPath, "simple.go"))
    50  		if err != nil {
    51  			log.Fatal(err)
    52  		}
    53  		text := string(content)
    54  		matched, err := regexp.MatchString(`"simple", func`, text)
    55  
    56  		if !matched {
    57  			fmt.Println(text)
    58  			t.Fatalf("File's content is not correct, %v", err)
    59  		}
    60  	})
    61  }