undocker

extract docker archives
Log | Files | Refs | README | LICENSE

tree_test.go (2324B) - Raw


      1 package rootfs
      2 
      3 import (
      4 	"testing"
      5 )
      6 
      7 func TestTree(t *testing.T) {
      8 	tests := []struct {
      9 		name       string
     10 		paths      []string
     11 		matchTrue  []string
     12 		matchFalse []string
     13 	}{
     14 		{
     15 			name:       "empty sequence matches nothing",
     16 			paths:      []string{},
     17 			matchFalse: []string{"a", "b"},
     18 		},
     19 		{
     20 			name:      "directory",
     21 			paths:     []string{"a/b"},
     22 			matchTrue: []string{"a/b/"},
     23 		},
     24 		{
     25 			name:       "a few sequences",
     26 			paths:      []string{"a", "b", "c/b/a"},
     27 			matchTrue:  []string{"a", "a/b/c", "c/b/a", "c/b/a/d"},
     28 			matchFalse: []string{"c/d", "c", "c/b"},
     29 		},
     30 	}
     31 
     32 	for _, tt := range tests {
     33 		t.Run(tt.name, func(t *testing.T) {
     34 			tree := newTree(tt.paths...)
     35 
     36 			for _, path := range tt.matchTrue {
     37 				t.Run(path, func(t *testing.T) {
     38 					if !tree.HasPrefix(path) {
     39 						t.Errorf("expected %s to be a prefix of %s", path, tree)
     40 					}
     41 				})
     42 			}
     43 
     44 			for _, path := range tt.matchFalse {
     45 				t.Run(path, func(t *testing.T) {
     46 					if tree.HasPrefix(path) {
     47 						t.Errorf("expected %s to not be a prefix of %s", path, tree)
     48 					}
     49 				})
     50 			}
     51 		})
     52 	}
     53 }
     54 
     55 func TestTreeMerge(t *testing.T) {
     56 	tree1 := newTree("bin/ar", "var/cache/apt")
     57 	tree2 := newTree("bin/ar", "bin/busybox", "usr/share/doc")
     58 	tree1.Merge(tree2)
     59 	want1 := "./bin/ar:./bin/busybox:./usr/share/doc:./var/cache/apt"
     60 	if got := tree1.String(); want1 != got {
     61 		t.Errorf("want != got: %q != %q", want1, got)
     62 	}
     63 	want2 := "./bin/ar:./bin/busybox:./usr/share/doc"
     64 	if got := tree2.String(); want2 != got {
     65 		t.Errorf("want != got: %q != %q", want2, got)
     66 	}
     67 }
     68 
     69 func TestString(t *testing.T) {
     70 	tests := []struct {
     71 		name    string
     72 		paths   []string
     73 		wantStr string
     74 	}{
     75 		{
     76 			name:    "empty",
     77 			paths:   []string{},
     78 			wantStr: "<empty>",
     79 		},
     80 		{
     81 			name:    "simple path",
     82 			paths:   []string{"a/b/c"},
     83 			wantStr: "./a/b/c",
     84 		},
     85 		{
     86 			name:    "duplicate paths",
     87 			paths:   []string{"a/a", "a//a"},
     88 			wantStr: "./a/a",
     89 		},
     90 		{
     91 			name:    "a few sequences",
     92 			paths:   []string{"bin/ar", "bin/busybox", "var/cache/apt/archives"},
     93 			wantStr: "./bin/ar:./bin/busybox:./var/cache/apt/archives",
     94 		},
     95 	}
     96 
     97 	for _, tt := range tests {
     98 		t.Run(tt.name, func(t *testing.T) {
     99 			tree := newTree(tt.paths...)
    100 			if tt.wantStr != tree.String() {
    101 				t.Errorf("want != got: %q != %q", tt.wantStr, tree.String())
    102 			}
    103 		})
    104 	}
    105 }