github.com/wawandco/oxpecker-plugins@v0.1.1/tools/pop/fixer_test.go (about)

     1  package pop
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  var (
    11  	ctx  context.Context
    12  	str  string
    13  	args []string
    14  )
    15  
    16  func TestFix(t *testing.T) {
    17  	t.Run("DataBaseExist", func(t *testing.T) {
    18  		err := os.Chdir(t.TempDir())
    19  		if err != nil {
    20  			t.Fatal("could not move to tmp folder")
    21  		}
    22  		content := []byte("")
    23  		err = ioutil.WriteFile("database.yml", content, 0755)
    24  		if err != nil {
    25  			t.Fatal("could not create file")
    26  		}
    27  
    28  		err = os.MkdirAll("config/", 0755)
    29  		if err != nil {
    30  			t.Fatal("could not create config folder")
    31  		}
    32  
    33  		f := Fixer{}
    34  		err = f.Fix(ctx, str, args)
    35  		if err != nil {
    36  			t.Fatalf("error should be nill, got %v", err)
    37  		}
    38  	})
    39  	t.Run("DataBaseNotExist", func(t *testing.T) {
    40  		err := os.Chdir(t.TempDir())
    41  		if err != nil {
    42  			t.Fatal("could not move to tmp folder")
    43  		}
    44  
    45  		f := Fixer{}
    46  		err = f.Fix(ctx, str, args)
    47  		if err != ErrDatabaseNotExist {
    48  			t.Fatalf("error should be %v, got %v", ErrDatabaseNotExist, err)
    49  		}
    50  	})
    51  }
    52  
    53  func TestFileExists(t *testing.T) {
    54  	t.Run("DataBaseExist", func(t *testing.T) {
    55  		err := os.Chdir(t.TempDir())
    56  		if err != nil {
    57  			t.Fatal("could not move to tmp folder")
    58  		}
    59  		content := []byte("")
    60  		err = ioutil.WriteFile("database.yml", content, 0755)
    61  		if err != nil {
    62  			t.Fatal("could not create file")
    63  		}
    64  
    65  		f := Fixer{}
    66  		_, err = f.fileExists(".")
    67  		if err != nil {
    68  			t.Fatalf("error should be nill, got %v", err)
    69  		}
    70  	})
    71  	t.Run("DataBaseNotExist", func(t *testing.T) {
    72  		err := os.Chdir(t.TempDir())
    73  		if err != nil {
    74  			t.Fatal("could not move to tmp folder")
    75  		}
    76  
    77  		f := Fixer{}
    78  		_, err = f.fileExists(".")
    79  		if err != ErrDatabaseNotExist {
    80  			t.Fatalf("error should be %v, got %v", ErrDatabaseNotExist, err)
    81  		}
    82  	})
    83  }
    84  
    85  func TestMoveFile(t *testing.T) {
    86  	t.Run("DataBaseExist", func(t *testing.T) {
    87  		err := os.Chdir(t.TempDir())
    88  		if err != nil {
    89  			t.Fatal("could not move to tmp folder")
    90  		}
    91  		content := []byte("")
    92  		err = ioutil.WriteFile("database.yml", content, 0755)
    93  		if err != nil {
    94  			t.Fatal("could not create file")
    95  		}
    96  
    97  		err = os.MkdirAll("config/", 0755)
    98  		if err != nil {
    99  			t.Fatal("could not create config folder")
   100  		}
   101  
   102  		f := Fixer{}
   103  		err = f.moveFile()
   104  		if err != nil {
   105  			t.Fatalf("error should be nill, got %v", err)
   106  		}
   107  	})
   108  	t.Run("DataBaseNotExist", func(t *testing.T) {
   109  		err := os.Chdir(t.TempDir())
   110  		if err != nil {
   111  			t.Fatal("could not move to tmp folder")
   112  		}
   113  
   114  		err = os.MkdirAll("config/", 0755)
   115  		if err != nil {
   116  			t.Fatal("could not create config folder")
   117  		}
   118  
   119  		f := Fixer{}
   120  		err = f.moveFile()
   121  		if err == nil {
   122  			t.Fatalf("error should be %v, got nil", err)
   123  		}
   124  	})
   125  }