github.com/msoap/go-carpet@v1.10.1-0.20240316220419-b690da179708/mod_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func Test_guessAbsPathInGoMod(t *testing.T) {
     9  	if err := os.Setenv("GO111MODULE", "on"); err != nil {
    10  		t.Fatalf("failed to set env: %s", err)
    11  	}
    12  
    13  	t.Run("empty", func(t *testing.T) {
    14  		if _, err := guessAbsPathInGoMod(""); err == nil {
    15  			t.Errorf("failed to test empty file")
    16  		}
    17  	})
    18  
    19  	t.Run("real", func(t *testing.T) {
    20  		gotAbsPath, err := guessAbsPathInGoMod("github.com/msoap/go-carpet/terminal_posix.go")
    21  		if err != nil {
    22  			t.Errorf("failed to test real file: %s", err)
    23  		}
    24  
    25  		if _, err := os.Stat(gotAbsPath); err != nil {
    26  			t.Errorf("failed to test real file: %s", err)
    27  		}
    28  	})
    29  
    30  	t.Run("not exists", func(t *testing.T) {
    31  		_, err := guessAbsPathInGoMod("github.com/msoap/go-carpet/terminal_posix_another_file.go")
    32  		if err == nil {
    33  			t.Errorf("failed to test not exists file")
    34  		}
    35  	})
    36  }