github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/standard/fixer_test.go (about)

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