github.com/dnephin/dobi@v0.15.0/tasks/job/build_test.go (about)

     1  package job
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/dnephin/dobi/config"
     8  	"github.com/google/go-cmp/cmp"
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  )
    12  
    13  func TestBuildDockerfileWithCopy(t *testing.T) {
    14  	mounts := []config.MountConfig{
    15  		{
    16  			Bind: ".",
    17  			Path: "/opt/var/foo",
    18  		},
    19  		{
    20  			Bind: "./dist",
    21  			Path: "/go/bin",
    22  		},
    23  	}
    24  	buf := buildDockerfileWithCopy("alpine:3.6", mounts)
    25  	expected := `FROM alpine:3.6
    26  COPY ./dist /go/bin
    27  COPY . /opt/var/foo
    28  `
    29  	assert.Check(t, is.Equal(expected, buf.String()))
    30  }
    31  
    32  func TestGetArtifactPath(t *testing.T) {
    33  	workingDir := "/work"
    34  	mounts := []config.MountConfig{
    35  		{
    36  			Bind: ".",
    37  			Path: "/go/src/github.com/dnephin/dobi",
    38  		},
    39  		{
    40  			Bind: "./dist/bin/",
    41  			Path: "/go/bin/",
    42  		},
    43  		{
    44  			Bind: ".env",
    45  			Path: "/code/.env",
    46  			File: true,
    47  		},
    48  	}
    49  
    50  	var testcases = []struct {
    51  		doc      string
    52  		glob     string
    53  		expected artifactPath
    54  	}{
    55  		{
    56  			doc:      "directory glob, exact match with mount",
    57  			glob:     "./dist/bin/",
    58  			expected: newArtifactPath("/work/dist/bin/", "/go/bin/", "/work/dist/bin/"),
    59  		},
    60  		{
    61  			doc:      "file glob, exact match with mount",
    62  			glob:     ".env",
    63  			expected: newArtifactPath("/work/.env", "/code/.env", "/work/.env"),
    64  		},
    65  	}
    66  	for _, testcase := range testcases {
    67  		t.Run(testcase.doc, func(t *testing.T) {
    68  			actual, err := getArtifactPath(workingDir, testcase.glob, mounts)
    69  			assert.NilError(t, err)
    70  			assert.Assert(t, is.DeepEqual(testcase.expected, actual, cmpArtifactPathOpt))
    71  		})
    72  	}
    73  }
    74  
    75  var cmpArtifactPathOpt = cmp.AllowUnexported(artifactPath{})
    76  
    77  func TestHasPathPrefix(t *testing.T) {
    78  	var testcases = []struct {
    79  		doc      string
    80  		path     string
    81  		prefix   string
    82  		expected bool
    83  	}{
    84  		{
    85  			doc:      "identical parts match",
    86  			path:     "/one/two/three",
    87  			prefix:   "/one/two/three",
    88  			expected: true,
    89  		},
    90  		{
    91  			doc:      "parts match with trailing slash",
    92  			path:     "/one/two/three/",
    93  			prefix:   "/one/two/three",
    94  			expected: true,
    95  		},
    96  		{
    97  			doc:      "parts match with trailing slash on prefix",
    98  			path:     "/one/two/three",
    99  			prefix:   "/one/two/three/",
   100  			expected: true,
   101  		},
   102  		{
   103  			doc:      "prefix match",
   104  			path:     "/one/two/three",
   105  			prefix:   "/one/two",
   106  			expected: true,
   107  		},
   108  		{
   109  			doc:    "item mismatch",
   110  			path:   "/one/two/three",
   111  			prefix: "/one/three/three",
   112  		},
   113  		{
   114  			doc:    "prefix longer mismatch",
   115  			path:   "/one/two/three",
   116  			prefix: "/one/two/three/four",
   117  		},
   118  	}
   119  	for _, testcase := range testcases {
   120  		actual := hasPathPrefix(testcase.path, testcase.prefix)
   121  		assert.Check(t, is.Equal(testcase.expected, actual), testcase.doc)
   122  	}
   123  }
   124  
   125  func TestArtifactPathContainerDir(t *testing.T) {
   126  	path := newArtifactPath("/work/dist/bin/", "/go/bin/", "/work/dist/bin/binary*")
   127  	assert.Check(t, is.Equal("/go/bin/", path.containerDir()))
   128  
   129  	path = newArtifactPath("/work/dist/bin/", "/go/bin/", "/work/dist/bin/")
   130  	assert.Check(t, is.Equal("/go/bin/", path.containerDir()))
   131  }
   132  
   133  func TestArtifactPathContainerGlob(t *testing.T) {
   134  	path := newArtifactPath("/work/dist/bin/", "/go/bin/", "/work/dist/bin/binary*")
   135  	assert.Check(t, is.Equal("/go/bin/binary*", path.containerGlob()))
   136  
   137  	path = newArtifactPath("/work/dist/bin/", "/go/bin/", "/work/dist/bin/")
   138  	assert.Check(t, is.Equal("/go/bin/", path.containerGlob()))
   139  }
   140  
   141  func TestArtifactPathHostPath(t *testing.T) {
   142  	path := newArtifactPath("/work/dist/bin/", "/go/bin/", "/work/dist/bin/")
   143  	containerPath := "/go/bin/dobi-darwin"
   144  	assert.Check(t, is.Equal("/work/dist/bin/dobi-darwin", path.hostPath(containerPath)))
   145  }
   146  
   147  func TestArtifactPathFromArchive(t *testing.T) {
   148  	var testcases = []struct {
   149  		artifactPath artifactPath
   150  		archivePath  string
   151  		expected     string
   152  	}{
   153  		{
   154  			artifactPath: newArtifactPath(
   155  				"/work/dist/bin/",
   156  				"/go/bin/",
   157  				"/work/dist/bin/"),
   158  			archivePath: "bin/dobi-darwin",
   159  			expected:    "/go/bin/dobi-darwin",
   160  		},
   161  		{
   162  			artifactPath: newArtifactPath(
   163  				"/work/",
   164  				"/go/src/github.com/dnephin/dobi/",
   165  				"/work/docs/build/html/"),
   166  			archivePath: "html/",
   167  			expected:    "/go/src/github.com/dnephin/dobi/docs/build/html/",
   168  		},
   169  		{
   170  			artifactPath: newArtifactPath(
   171  				"/work/",
   172  				"/go/src/github.com/dnephin/dobi/",
   173  				"/work/docs/build/html/"),
   174  			archivePath: "html/foo/file",
   175  			expected:    "/go/src/github.com/dnephin/dobi/docs/build/html/foo/file",
   176  		},
   177  	}
   178  
   179  	for _, testcase := range testcases {
   180  		actual := testcase.artifactPath.pathFromArchive(testcase.archivePath)
   181  		assert.Check(t, is.Equal(testcase.expected, actual))
   182  	}
   183  }
   184  
   185  func TestFileMatchesGlob(t *testing.T) {
   186  	var testcases = []struct {
   187  		glob     string
   188  		path     string
   189  		expected bool
   190  	}{
   191  		{
   192  			glob:     "/go/bin/",
   193  			path:     "/go/bin/foo/bar",
   194  			expected: true,
   195  		},
   196  		{
   197  			glob: "/go/bin",
   198  			path: "/go/bin/foo/bar",
   199  		},
   200  		{
   201  			glob:     "/work/foo-*",
   202  			path:     "/work/foo-one",
   203  			expected: true,
   204  		},
   205  		{
   206  			glob: "/work/foo-*",
   207  			path: "/work/foo-one/two",
   208  		},
   209  	}
   210  
   211  	for _, testcase := range testcases {
   212  		doc := fmt.Sprintf("path: %s glob: %s", testcase.path, testcase.glob)
   213  		match, err := fileMatchesGlob(testcase.path, testcase.glob)
   214  		if assert.Check(t, err, doc) {
   215  			assert.Check(t, is.Equal(testcase.expected, match), doc)
   216  		}
   217  	}
   218  }