github.com/adevinta/lava@v0.7.2/internal/gitserver/gittest/gittest_test.go (about)

     1  // Copyright 2023 Adevinta
     2  
     3  package gittest
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestExtractTemp(t *testing.T) {
    12  	tests := []struct {
    13  		name       string
    14  		tarfile    string
    15  		wantNilErr bool
    16  	}{
    17  		{
    18  			name:       "valid",
    19  			tarfile:    "testrepo.tar",
    20  			wantNilErr: true,
    21  		},
    22  		{
    23  			name:       "invalid path",
    24  			tarfile:    "notfound.tar",
    25  			wantNilErr: false,
    26  		},
    27  		{
    28  			name:       "invalid tar file",
    29  			tarfile:    "invalid.tar",
    30  			wantNilErr: false,
    31  		},
    32  	}
    33  
    34  	for _, tt := range tests {
    35  		t.Run(tt.name, func(t *testing.T) {
    36  			tmpPath, err := ExtractTemp(filepath.Join("testdata", tt.tarfile))
    37  			if err != nil {
    38  				if tt.wantNilErr {
    39  					t.Fatalf("unexpected extract error: %v", err)
    40  				}
    41  				return
    42  			}
    43  			defer os.RemoveAll(tmpPath)
    44  
    45  			if _, err := os.Stat(filepath.Join(tmpPath, "foo.txt")); err != nil {
    46  				t.Fatalf("unexpected stat error: %v", err)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestCloneTemp(t *testing.T) {
    53  	tmpPath, err := ExtractTemp(filepath.Join("testdata", "testrepo.tar"))
    54  	if err != nil {
    55  		t.Fatalf("unexpected extract error: %v", err)
    56  	}
    57  	defer os.RemoveAll(tmpPath)
    58  
    59  	clonePath, err := CloneTemp(tmpPath)
    60  	if err != nil {
    61  		t.Fatalf("unexpected clone error: %v", err)
    62  	}
    63  
    64  	if _, err := os.Stat(filepath.Join(clonePath, "foo.txt")); err != nil {
    65  		t.Fatalf("unexpected stat error: %v", err)
    66  	}
    67  }
    68  
    69  func TestCloneTemp_invalid_repo(t *testing.T) {
    70  	if _, err := CloneTemp("testdata"); err == nil {
    71  		t.Fatalf("unexpected nil error")
    72  	}
    73  }