github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/pkg/binspec/binspec_test.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package binspec_test
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path"
    21  	"regexp"
    22  	"testing"
    23  
    24  	"github.com/nmiyake/pkg/dirs"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"github.com/palantir/godel/apps/distgo/pkg/binspec"
    29  	"github.com/palantir/godel/apps/distgo/pkg/osarch"
    30  )
    31  
    32  func TestBinSpecCreateDirectoryStructureFailBadRootDirectory(t *testing.T) {
    33  	tmp, cleanup, err := dirs.TempDir("", "")
    34  	defer cleanup()
    35  	require.NoError(t, err)
    36  
    37  	spec := binspec.New([]osarch.OSArch{
    38  		{
    39  			OS:   "darwin",
    40  			Arch: "amd-64",
    41  		},
    42  	}, "testExecutable")
    43  	err = spec.CreateDirectoryStructure(tmp, nil, false)
    44  	require.Error(t, err)
    45  
    46  	assert.Regexp(t, regexp.MustCompile(`^.+ is not a path to bin$`), err.Error())
    47  }
    48  
    49  func TestBinSpecCreateStructure(t *testing.T) {
    50  	tmp, cleanup, err := dirs.TempDir("", "")
    51  	defer cleanup()
    52  	require.NoError(t, err)
    53  
    54  	for i, currCase := range []struct {
    55  		input         []osarch.OSArch
    56  		expectedPaths []string
    57  	}{
    58  		{
    59  			input:         []osarch.OSArch{{OS: "darwin", Arch: "amd64"}},
    60  			expectedPaths: []string{"bin/darwin-amd64"},
    61  		},
    62  		{
    63  			input:         []osarch.OSArch{{OS: "darwin", Arch: "amd64"}, {OS: "linux", Arch: "amd64"}},
    64  			expectedPaths: []string{"bin/darwin-amd64", "bin/linux-amd64"},
    65  		},
    66  	} {
    67  		currTmpDir, err := ioutil.TempDir(tmp, "")
    68  		require.NoError(t, err)
    69  
    70  		currRoot := path.Join(currTmpDir, "bin")
    71  		err = os.Mkdir(currRoot, 0755)
    72  		require.NoError(t, err)
    73  
    74  		spec := binspec.New(currCase.input, "testExecutable")
    75  		err = spec.CreateDirectoryStructure(currRoot, nil, false)
    76  		require.NoError(t, err)
    77  
    78  		for _, currPath := range currCase.expectedPaths {
    79  			_, err := os.Stat(path.Join(currTmpDir, currPath))
    80  			assert.NoError(t, err, "Case %d", i)
    81  		}
    82  	}
    83  }