...

Source file src/gotest.tools/v3/fs/manifest_test.go

Documentation: gotest.tools/v3/fs

     1  package fs
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  	"gotest.tools/v3/assert"
    13  )
    14  
    15  func TestManifestFromDir(t *testing.T) {
    16  	var defaultFileMode os.FileMode = 0644
    17  	var subDirMode = 0755 | os.ModeDir
    18  	var jFileMode os.FileMode = 0600
    19  	if runtime.GOOS == "windows" {
    20  		defaultFileMode = 0666
    21  		subDirMode = 0777 | os.ModeDir
    22  		jFileMode = 0666
    23  	}
    24  
    25  	var userOps []PathOp
    26  	var expectedUserResource = newResource(defaultFileMode)
    27  	if os.Geteuid() == 0 {
    28  		userOps = append(userOps, AsUser(1001, 1002))
    29  		expectedUserResource = resource{mode: defaultFileMode, uid: 1001, gid: 1002}
    30  	}
    31  
    32  	srcDir := NewDir(t, t.Name(),
    33  		WithFile("j", "content j", WithMode(0600)),
    34  		WithDir("s",
    35  			WithFile("k", "content k")),
    36  		WithSymlink("f", "j"),
    37  		WithFile("x", "content x", userOps...))
    38  	defer srcDir.Remove()
    39  
    40  	expected := Manifest{
    41  		root: &directory{
    42  			resource: newResource(defaultRootDirMode),
    43  			items: map[string]dirEntry{
    44  				"j": &file{
    45  					resource: newResource(jFileMode),
    46  					content:  readCloser("content j"),
    47  				},
    48  				"s": &directory{
    49  					resource: newResource(subDirMode),
    50  					items: map[string]dirEntry{
    51  						"k": &file{
    52  							resource: newResource(defaultFileMode),
    53  							content:  readCloser("content k"),
    54  						},
    55  					},
    56  					filepathGlobs: map[string]*filePath{},
    57  				},
    58  				"f": &symlink{
    59  					resource: newResource(defaultSymlinkMode),
    60  					target:   srcDir.Join("j"),
    61  				},
    62  				"x": &file{
    63  					resource: expectedUserResource,
    64  					content:  readCloser("content x"),
    65  				},
    66  			},
    67  			filepathGlobs: map[string]*filePath{},
    68  		},
    69  	}
    70  	actual := ManifestFromDir(t, srcDir.Path())
    71  	assert.DeepEqual(t, actual, expected, cmpManifest)
    72  	actual.root.items["j"].(*file).content.Close()
    73  	actual.root.items["x"].(*file).content.Close()
    74  	actual.root.items["s"].(*directory).items["k"].(*file).content.Close()
    75  }
    76  
    77  func TestSymlinks(t *testing.T) {
    78  	rootDirectory := NewDir(t, "root",
    79  		WithFile("foo.txt", "foo"),
    80  		WithSymlink("foo.link", "foo.txt"))
    81  	defer rootDirectory.Remove()
    82  	expected := Expected(t,
    83  		WithFile("foo.txt", "foo"),
    84  		WithSymlink("foo.link", rootDirectory.Join("foo.txt")))
    85  	assert.Assert(t, Equal(rootDirectory.Path(), expected))
    86  }
    87  
    88  var cmpManifest = cmp.Options{
    89  	cmp.AllowUnexported(Manifest{}, resource{}, file{}, symlink{}, directory{}),
    90  	cmp.Comparer(func(x, y io.ReadCloser) bool {
    91  		if x == nil || y == nil {
    92  			return x == y
    93  		}
    94  		xContent, err := io.ReadAll(x)
    95  		if err != nil {
    96  			return false
    97  		}
    98  
    99  		yContent, err := io.ReadAll(y)
   100  		if err != nil {
   101  			return false
   102  		}
   103  		return bytes.Equal(xContent, yContent)
   104  	}),
   105  }
   106  
   107  func readCloser(s string) io.ReadCloser {
   108  	return io.NopCloser(strings.NewReader(s))
   109  }
   110  

View as plain text