github.com/bingoohuang/pkger@v0.0.0-20210127185155-a71b9df4c4c7/parser/parser_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/bingoohuang/pkger/here"
    12  	"github.com/bingoohuang/pkger/pkging/pkgtest"
    13  	"github.com/bingoohuang/pkger/pkging/stdos"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func Test_Parser_Ref(t *testing.T) {
    18  	defer func() {
    19  		c := exec.Command("go", "mod", "tidy", "-v")
    20  		c.Run()
    21  	}()
    22  	r := require.New(t)
    23  
    24  	ref, err := pkgtest.NewRef()
    25  	r.NoError(err)
    26  	defer os.RemoveAll(ref.Dir)
    27  
    28  	disk, err := stdos.New(ref.Info)
    29  	r.NoError(err)
    30  
    31  	_, err = pkgtest.LoadFiles("/", ref, disk)
    32  	r.NoError(err)
    33  
    34  	res, err := Parse(ref.Info)
    35  
    36  	r.NoError(err)
    37  
    38  	files, err := res.Files()
    39  	r.NoError(err)
    40  	for _, f := range files {
    41  		fmt.Println(f.Path)
    42  	}
    43  	r.Len(files, 25)
    44  }
    45  
    46  func Test_Parser_Ref_Include(t *testing.T) {
    47  	defer func() {
    48  		c := exec.Command("go", "mod", "tidy", "-v")
    49  		c.Run()
    50  	}()
    51  	r := require.New(t)
    52  
    53  	ref, err := pkgtest.NewRef()
    54  	r.NoError(err)
    55  	defer os.RemoveAll(ref.Dir)
    56  
    57  	disk, err := stdos.New(ref.Info)
    58  	r.NoError(err)
    59  
    60  	_, err = pkgtest.LoadFiles("/", ref, disk)
    61  	r.NoError(err)
    62  
    63  	res, err := Parse(ref.Info, "github.com/stretchr/testify:/go.mod")
    64  
    65  	r.NoError(err)
    66  
    67  	files, err := res.Files()
    68  	r.NoError(err)
    69  
    70  	l := len(files)
    71  	r.Equal(26, l)
    72  }
    73  
    74  func Test_Parser_dotGo_Directory(t *testing.T) {
    75  	r := require.New(t)
    76  
    77  	ref, err := pkgtest.NewRef()
    78  	r.NoError(err)
    79  	defer os.RemoveAll(ref.Dir)
    80  
    81  	err = os.Mkdir(filepath.Join(ref.Dir, ".go"), 0755)
    82  	r.NoError(err)
    83  
    84  	disk, err := stdos.New(ref.Info)
    85  	r.NoError(err)
    86  
    87  	_, err = pkgtest.LoadFiles("/", ref, disk)
    88  	r.NoError(err)
    89  
    90  	res, err := Parse(ref.Info)
    91  	r.NoError(err)
    92  	r.Equal(11, len(res))
    93  }
    94  
    95  func Test_Parser_Example_HTTP(t *testing.T) {
    96  	r := require.New(t)
    97  
    98  	cur, err := here.Package("github.com/bingoohuang/pkger")
    99  	r.NoError(err)
   100  
   101  	root := filepath.Join(cur.Dir, "examples", "http", "pkger")
   102  
   103  	defer func() {
   104  		c := exec.Command("go", "mod", "tidy", "-v")
   105  		c.Run()
   106  	}()
   107  
   108  	her, err := here.Dir(root)
   109  	r.NoError(err)
   110  
   111  	res, err := Parse(her)
   112  	r.NoError(err)
   113  
   114  	files, err := res.Files()
   115  	r.NoError(err)
   116  	r.Len(files, 5)
   117  
   118  	for _, f := range files {
   119  		r.True(strings.HasPrefix(f.Abs, her.Dir), "%q %q", f.Abs, her.Dir)
   120  		r.True(strings.HasPrefix(f.Path.Name, "/public"), "%q %q", f.Path.Name, "/public")
   121  	}
   122  }