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

     1  package standard
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  func TestFix(t *testing.T) {
    13  	t.Run("MainAndGoModExist", func(t *testing.T) {
    14  		err := os.Chdir(t.TempDir())
    15  		if err != nil {
    16  			t.Fatal("could not move to tmp folder")
    17  		}
    18  
    19  		content := []byte("package main // Generated!")
    20  		err = ioutil.WriteFile("main.go", content, 0600)
    21  		if err != nil {
    22  			t.Fatal("could not create main file")
    23  		}
    24  
    25  		content = []byte("module github.com/some/cool/package")
    26  		err = ioutil.WriteFile("go.mod", content, 0600)
    27  		if err != nil {
    28  			t.Fatalf("could not create go.mod file: %v", err)
    29  		}
    30  		f := Fixer{}
    31  		err = f.Fix(context.Background(), "", []string{})
    32  		if err != nil {
    33  			t.Fatalf("error should be nill, got %v", err)
    34  		}
    35  
    36  	})
    37  	t.Run("MainMissing", func(t *testing.T) {
    38  		err := os.Chdir(t.TempDir())
    39  		if err != nil {
    40  			t.Fatal("could not move to tmp folder")
    41  		}
    42  
    43  		f := Fixer{}
    44  		err = f.Fix(context.Background(), "", []string{})
    45  		if err != ErrFileMainNotExist {
    46  			t.Fatalf("error should be nill, got %v", err)
    47  		}
    48  
    49  	})
    50  	t.Run("GoModMissing", func(t *testing.T) {
    51  		err := os.Chdir(t.TempDir())
    52  		if err != nil {
    53  			t.Fatal("could not move to tmp folder")
    54  		}
    55  
    56  		content := []byte("package main // Generated!")
    57  		err = ioutil.WriteFile("main.go", content, 0600)
    58  		if err != nil {
    59  			t.Fatal("could not create main file")
    60  		}
    61  
    62  		f := Fixer{}
    63  		err = f.Fix(context.Background(), "", []string{})
    64  		if err == nil {
    65  			t.Fatalf("error should be %v, got nil", err)
    66  		}
    67  
    68  	})
    69  }
    70  
    71  func TestMoveFile(t *testing.T) {
    72  
    73  	t.Run("MainExists", func(t *testing.T) {
    74  		err := os.Chdir(t.TempDir())
    75  		if err != nil {
    76  			t.Fatal("could not move to tmp folder")
    77  		}
    78  
    79  		content := []byte("package main // Generated!")
    80  		err = ioutil.WriteFile("main.go", content, 0600)
    81  		if err != nil {
    82  			t.Fatal("could not create main file")
    83  		}
    84  
    85  		f := Fixer{}
    86  		err = f.moveFile("julian")
    87  		if err != nil {
    88  			t.Fatal("movefile did not work")
    89  		}
    90  
    91  		dat, err := ioutil.ReadFile(filepath.Join("cmd", "julian", "main.go"))
    92  		if err != nil {
    93  			t.Fatal("movefile did not work (reading file)")
    94  		}
    95  
    96  		if !bytes.Contains(dat, content) {
    97  			t.Fatal("did not move content correctly")
    98  		}
    99  	})
   100  
   101  	t.Run("ModuleNameEmpty", func(t *testing.T) {
   102  		err := os.Chdir(t.TempDir())
   103  		if err != nil {
   104  			t.Fatal("could not move to tmp folder")
   105  		}
   106  
   107  		content := []byte("package main // Generated!")
   108  		err = ioutil.WriteFile("main.go", content, 0600)
   109  		if err != nil {
   110  			t.Fatal("could not create main file")
   111  		}
   112  
   113  		f := Fixer{}
   114  		err = f.moveFile("")
   115  		if err != ErrModuleNameNeeded {
   116  			t.Fatalf("needs to return ErrModuleNameNeeded, got %v", err)
   117  		}
   118  	})
   119  
   120  	t.Run("MainMissing", func(t *testing.T) {
   121  		err := os.Chdir(t.TempDir())
   122  		if err != nil {
   123  			t.Fatal("could not move to tmp folder")
   124  		}
   125  
   126  		f := Fixer{}
   127  		err = f.moveFile("julian")
   128  		if err == nil {
   129  			t.Fatal("movefile did work")
   130  		}
   131  	})
   132  
   133  	t.Run("MainMissing", func(t *testing.T) {
   134  		err := os.Chdir(t.TempDir())
   135  		if err != nil {
   136  			t.Fatal("could not move to tmp folder")
   137  		}
   138  
   139  		content := []byte("package main // Generated!")
   140  		err = ioutil.WriteFile("main.go", content, 0600)
   141  		if err != nil {
   142  			t.Fatal("could not create main file")
   143  		}
   144  
   145  		err = os.MkdirAll("cmd", 0755)
   146  		if err != nil {
   147  			t.Fatal("did not create cmd folder")
   148  		}
   149  
   150  		f := Fixer{}
   151  		err = f.moveFile("julian")
   152  		if err != nil {
   153  			t.Fatal("movefile did work")
   154  		}
   155  
   156  		_, err = os.Stat(filepath.Join("cmd", "julian", "main.go"))
   157  		if err != nil {
   158  			t.Fatalf("should have worked, got %v", err)
   159  		}
   160  	})
   161  }
   162  
   163  func TestFileExists(t *testing.T) {
   164  
   165  	t.Run("MainExist", func(t *testing.T) {
   166  		err := os.Chdir(t.TempDir())
   167  		if err != nil {
   168  			t.Fatal("could not move to tmp folder")
   169  		}
   170  
   171  		content := []byte("package main // Generated!")
   172  		err = ioutil.WriteFile("main.go", content, 0600)
   173  		if err != nil {
   174  			t.Fatal("could not create main file")
   175  		}
   176  
   177  		f := Fixer{}
   178  		_, err = f.fileExists()
   179  		if err != nil {
   180  			t.Fatalf("Should found file, got %v", err)
   181  		}
   182  	})
   183  	t.Run("MainNotExist", func(t *testing.T) {
   184  		err := os.Chdir(t.TempDir())
   185  		if err != nil {
   186  			t.Fatal("could not move to tmp folder")
   187  		}
   188  
   189  		f := Fixer{}
   190  		found, err := f.fileExists()
   191  		if err != ErrFileMainNotExist {
   192  			t.Fatalf("Should return %v, got %v", ErrFileMainNotExist, err)
   193  		}
   194  
   195  		if found {
   196  			t.Fatalf("should have not found the file")
   197  		}
   198  	})
   199  }