github.com/wawandco/oxplugins@v0.7.11/lifecycle/new/command_test.go (about)

     1  package new_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/wawandco/oxplugins/lifecycle/new"
    10  	"github.com/wawandco/oxplugins/plugins"
    11  )
    12  
    13  func TestRun(t *testing.T) {
    14  	root := t.TempDir()
    15  	err := os.Chdir(root)
    16  	if err != nil {
    17  		t.Error("could not change to temp directory")
    18  	}
    19  
    20  	pl := &new.Command{}
    21  	tinit := &Tinit{}
    22  	pl.Receive([]plugins.Plugin{tinit})
    23  
    24  	err = pl.Run(context.Background(), root, []string{})
    25  	if err == nil {
    26  		t.Error("should return an error")
    27  	}
    28  
    29  	err = pl.Run(context.Background(), root, []string{"app"})
    30  	if err != nil {
    31  		t.Errorf("should not return and error, got: %v", err)
    32  	}
    33  
    34  	//Should create the folder
    35  	fi, err := os.Stat(filepath.Join(root, "app"))
    36  	if err != nil {
    37  		t.Errorf("should not return and error, got: %v", err)
    38  	}
    39  
    40  	if !fi.IsDir() {
    41  		t.Errorf("should be a folder, got a file")
    42  	}
    43  
    44  	if !tinit.called {
    45  		t.Errorf("should have called initializer")
    46  	}
    47  
    48  	if !tinit.afterCalled {
    49  		t.Errorf("should have called afterinitialize")
    50  	}
    51  
    52  	if tinit.root != filepath.Join(root, "app") {
    53  		t.Errorf("should call initializer with root being: %v", filepath.Join(root, "app"))
    54  	}
    55  }
    56  
    57  func TestFolderName(t *testing.T) {
    58  	tcases := []struct {
    59  		args     []string
    60  		expected string
    61  	}{
    62  		{[]string{"aaa"}, "aaa"},
    63  		{[]string{"something/aaa"}, "aaa"},
    64  		{[]string{"something\\aaa"}, "something\\aaa"},
    65  	}
    66  
    67  	pl := &new.Command{}
    68  	for _, tcase := range tcases {
    69  		name := pl.FolderName(tcase.args)
    70  		if name != tcase.expected {
    71  			t.Errorf("should return %v got %v", tcase.expected, name)
    72  		}
    73  	}
    74  
    75  }