github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/utils/pip/dependencies/cache_test.go (about)

     1  package dependencies
     2  
     3  import (
     4  	"errors"
     5  	"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestDependenciesCache(t *testing.T) {
    13  	// Change test's work directory, rollback after function returns.
    14  	wd, _ := os.Getwd()
    15  	tmpTestPath := filepath.Join(os.TempDir(), "cacheTest")
    16  	err := os.MkdirAll(tmpTestPath, os.ModePerm)
    17  	if err != nil {
    18  		t.Error("Failed mkDirAll: " + err.Error())
    19  	}
    20  	err = os.Chdir(tmpTestPath)
    21  	if err != nil {
    22  		t.Error("Failed Chdir: " + err.Error())
    23  	}
    24  	defer func() {
    25  		os.RemoveAll(tmpTestPath)
    26  		os.Chdir(wd)
    27  	}()
    28  
    29  	cacheMap := make(map[string]*buildinfo.Dependency)
    30  	csA := buildinfo.Checksum{Sha1: "sha1A", Md5: "md5A"}
    31  	depenA := buildinfo.Dependency{
    32  		Id:       "depenA-1.0-A.zip",
    33  		Checksum: &csA,
    34  	}
    35  	cacheMap["A"] = &depenA
    36  	csC := buildinfo.Checksum{Sha1: "sha1C", Md5: "md5C"}
    37  	depenC := buildinfo.Dependency{
    38  		Id:       "depenC-3.4-C.gzip",
    39  		Checksum: &csC,
    40  	}
    41  	cacheMap["C"] = &depenC
    42  	err = UpdateDependenciesCache(cacheMap)
    43  	if err != nil {
    44  		t.Error("Failed creating dependencies cache: " + err.Error())
    45  	}
    46  	cache, err := readCacheAndCheckError()
    47  	if err != nil {
    48  		t.Error("Failed reading dependencies cache: " + err.Error())
    49  	}
    50  
    51  	if !reflect.DeepEqual(*cache.GetDependency("A"), depenA) {
    52  		t.Error("Failed retrieving dependency A!!!")
    53  	}
    54  	if cache.GetDependency("B") != nil {
    55  		t.Error("Retrieving non-existing dependency B should return nil.")
    56  	}
    57  	if !reflect.DeepEqual(*cache.GetDependency("C"), depenC) {
    58  		t.Error("Failed retrieving dependency C!!!")
    59  	}
    60  	if cache.GetDependency("T") != nil {
    61  		t.Error("Retrieving non-existing dependency T should return nil checksum.")
    62  	}
    63  
    64  	delete(cacheMap, "A")
    65  	csT := buildinfo.Checksum{Sha1: "sha1T", Md5: "md5T"}
    66  	depenT := buildinfo.Dependency{
    67  		Id:       "depenT-6.0.68-T.zip",
    68  		Checksum: &csT,
    69  	}
    70  	cacheMap["T"] = &depenT
    71  	err = UpdateDependenciesCache(cacheMap)
    72  	if err != nil {
    73  		t.Error("Failed creating dependencies cache: " + err.Error())
    74  	}
    75  
    76  	cache, err = readCacheAndCheckError()
    77  	if err != nil {
    78  		t.Error("Failed reading dependencies cache: " + err.Error())
    79  	}
    80  	if cache.GetDependency("A") != nil {
    81  		t.Error("Retrieving non-existing dependency T should return nil checksum.")
    82  	}
    83  	if !reflect.DeepEqual(*cache.GetDependency("T"), depenT) {
    84  		t.Error("Failed retrieving dependency T.")
    85  	}
    86  	if !reflect.DeepEqual(*cache.GetDependency("C"), depenC) {
    87  		t.Error("Failed retrieving dependency C.")
    88  	}
    89  }
    90  
    91  func readCacheAndCheckError() (cache *DependenciesCache, err error) {
    92  	cache, err = GetProjectDependenciesCache()
    93  	if err != nil {
    94  		return
    95  	}
    96  	if cache == nil {
    97  		err = errors.New("Cache file does not exist.")
    98  	}
    99  	return
   100  }