github.com/visualfc/goembed@v0.3.3/embed_test.go (about)

     1  //go:build go1.16
     2  // +build go1.16
     3  
     4  package goembed
     5  
     6  import (
     7  	"embed"
     8  	"fmt"
     9  	"go/ast"
    10  	"go/build"
    11  	"go/parser"
    12  	"go/token"
    13  	"path/filepath"
    14  	"runtime"
    15  	"strings"
    16  	"testing"
    17  	"unsafe"
    18  )
    19  
    20  //go:embed testdata/data1.txt
    21  var data1 string
    22  
    23  var (
    24  	//go:embed testdata/data2.txt
    25  	data2 []byte
    26  
    27  	//go:embed testdata
    28  	fs embed.FS
    29  )
    30  
    31  type file struct {
    32  	name string
    33  	data string
    34  	hash [16]byte
    35  }
    36  
    37  type myfs struct {
    38  	files *[]file
    39  }
    40  
    41  func TestEmbed(t *testing.T) {
    42  	if data1 != "hello data1" {
    43  		t.Fail()
    44  	}
    45  	if string(data2) != "hello data2" {
    46  		t.Fail()
    47  	}
    48  	files := *(*myfs)(unsafe.Pointer(&fs)).files
    49  	for _, file := range files {
    50  		t.Log(file.name, file.data, file.hash)
    51  	}
    52  }
    53  
    54  func TestResolve(t *testing.T) {
    55  	pkg, err := build.Import("github.com/visualfc/goembed", "", 0)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	fset := token.NewFileSet()
    60  	var files []*ast.File
    61  	for _, file := range pkg.TestGoFiles {
    62  		f, err := parser.ParseFile(fset, filepath.Join(pkg.Dir, file), nil, 0)
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  		files = append(files, f)
    67  	}
    68  	ems, err := CheckEmbed(pkg.TestEmbedPatternPos, fset, files)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	r := NewResolve()
    73  	var checkData1 bool
    74  	var checkData2 bool
    75  	var checkFS bool
    76  	for _, em := range ems {
    77  		files, err := r.Load(pkg.Dir, fset, em)
    78  		if err != nil {
    79  			t.Fatal("error load", em, err)
    80  		}
    81  		if em.Name == "data1" {
    82  			checkData1 = true
    83  			if string(files[0].Data) != "hello data1" {
    84  				t.Fail()
    85  			}
    86  		} else if em.Name == "data2" {
    87  			checkData2 = true
    88  			if string(files[0].Data) != "hello data2" {
    89  				t.Fail()
    90  			}
    91  		}
    92  		if em.Kind == EmbedFiles && em.Name == "fs" {
    93  			checkFS = true
    94  			files := BuildFS(files)
    95  			for _, f := range files {
    96  				t.Log(f.Name, string(f.Data), f.Hash)
    97  			}
    98  			var info1 []string
    99  			var info2 []string
   100  			mfiles := *(*myfs)(unsafe.Pointer(&fs)).files
   101  			switch runtime.Version()[:6] {
   102  			default:
   103  				for _, file := range mfiles {
   104  					info1 = append(info1, fmt.Sprintf("%v,%v,%v", file.name, file.data, file.hash))
   105  				}
   106  				for _, f := range files {
   107  					info2 = append(info2, fmt.Sprintf("%v,%v,%v", f.Name, string(f.Data), f.Hash))
   108  				}
   109  			case "go1.19":
   110  				t.Log("go1.19 compiler use NOTSHA256 skip hash check")
   111  				for _, file := range mfiles {
   112  					info1 = append(info1, fmt.Sprintf("%v,%v", file.name, file.data))
   113  				}
   114  				for _, f := range files {
   115  					info2 = append(info2, fmt.Sprintf("%v,%v", f.Name, string(f.Data)))
   116  				}
   117  			}
   118  			if strings.Join(info1, ";") != strings.Join(info2, ";") {
   119  				t.Fatalf("build fs error:\n%v\n%v", info1, info2)
   120  			}
   121  		}
   122  	}
   123  	if !checkData1 || !checkData2 || !checkFS {
   124  		t.Fatal("not found embed", checkData1, checkData2, checkFS)
   125  	}
   126  }
   127  
   128  func TestBytesHex(t *testing.T) {
   129  	data := []byte("\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64")
   130  	s := BytesToHex(data)
   131  	if s != `\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64` {
   132  		t.Fatal(s)
   133  	}
   134  	if string(data) != "hello world" {
   135  		t.Fail()
   136  	}
   137  }
   138  
   139  func TestBytesList(t *testing.T) {
   140  	data := []byte{104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100}
   141  	s := BytesToList(data)
   142  	if s != `104,101,108,108,111,32,119,111,114,108,100` {
   143  		t.Fatal(s)
   144  	}
   145  	if string(data) != "hello world" {
   146  		t.Fail()
   147  	}
   148  }