github.com/goplus/gogen@v1.16.0/packages/imp_test.go (about)

     1  /*
     2   Copyright 2022 The GoPlus Authors (goplus.org)
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6       http://www.apache.org/licenses/LICENSE-2.0
     7   Unless required by applicable law or agreed to in writing, software
     8   distributed under the License is distributed on an "AS IS" BASIS,
     9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10   See the License for the specific language governing permissions and
    11   limitations under the License.
    12  */
    13  
    14  package packages
    15  
    16  import (
    17  	"go/types"
    18  	"io"
    19  	"os"
    20  	"testing"
    21  )
    22  
    23  func TestImporterNormal(t *testing.T) {
    24  	p := NewImporter(nil)
    25  	pkg, err := p.Import("fmt")
    26  	if err != nil || pkg.Path() != "fmt" {
    27  		t.Fatal("Import failed:", pkg, err)
    28  	}
    29  	if _, err = p.Import("not-found"); err == nil {
    30  		t.Fatal("Import not-found: no error?")
    31  	}
    32  	if pkg2, err := p.Import("fmt"); err != nil || pkg2 != pkg {
    33  		t.Fatal("Import reuse fail:", pkg, pkg2)
    34  	}
    35  }
    36  
    37  func TestImporterRecursive(t *testing.T) {
    38  	p := NewImporter(nil, "..")
    39  	pkg, err := p.Import("github.com/goplus/gogen/internal/foo")
    40  	if err != nil {
    41  		t.Fatal("Import failed:", pkg, err)
    42  	}
    43  }
    44  
    45  func TestImportBuiltin(t *testing.T) {
    46  	p := NewImporter(nil, "..")
    47  	pkg, err := p.Import("github.com/goplus/gogen/internal/builtin")
    48  	if err != nil {
    49  		t.Fatal("Import failed:", pkg, err)
    50  	}
    51  }
    52  
    53  func Test_loadByExport(t *testing.T) {
    54  	p := NewImporter(nil)
    55  	if _, err := loadByExport(p, "/not-found", "notfound"); !os.IsNotExist(err) {
    56  		t.Fatal("Test_loadByExport:", err)
    57  	}
    58  	if _, err := p.findExport(".", "C"); err == nil {
    59  		t.Fatal("Test_loadByExport: no error?")
    60  	}
    61  }
    62  
    63  func loadByExport(p *Importer, expfile, pkgPath string) (pkg *types.Package, err error) {
    64  	f, err := os.Open(expfile)
    65  	if err != nil {
    66  		return
    67  	}
    68  	return p.loadByExport(f, pkgPath)
    69  }
    70  
    71  // ----------------------------------------------------------------------------
    72  
    73  type diskCache struct {
    74  	imp *Importer
    75  }
    76  
    77  func (p diskCache) Find(dir, pkgPath string) (f io.ReadCloser, err error) {
    78  	if p.imp != nil {
    79  		return p.imp.findExport(dir, pkgPath)
    80  	}
    81  	return nil, os.ErrNotExist
    82  }
    83  
    84  func TestCache(t *testing.T) {
    85  	nlist = 0
    86  	p := NewImporter(nil)
    87  	p.SetCache(diskCache{})
    88  	_, err := p.Import("fmt")
    89  	if err != os.ErrNotExist {
    90  		t.Fatal("Import:", err)
    91  	}
    92  	p.SetCache(diskCache{imp: NewImporter(nil)})
    93  	if p.Cache() == nil {
    94  		t.Fatal("Cache nil")
    95  	}
    96  	pkg, err := p.Import("fmt")
    97  	if err != nil || pkg.Path() != "fmt" {
    98  		t.Fatal("Import fmt:", pkg, err)
    99  	}
   100  	if v := ListTimes(); v != 1 {
   101  		t.Fatal("ListTimes:", v)
   102  	}
   103  }
   104  
   105  // ----------------------------------------------------------------------------