github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/apps/distgo/cmd/publish/publish_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 publish_test
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path"
    22  	"runtime"
    23  	"testing"
    24  
    25  	"github.com/nmiyake/pkg/dirs"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"github.com/palantir/godel/apps/distgo/cmd/build"
    30  	"github.com/palantir/godel/apps/distgo/cmd/dist"
    31  	"github.com/palantir/godel/apps/distgo/cmd/publish"
    32  	"github.com/palantir/godel/apps/distgo/params"
    33  	"github.com/palantir/godel/apps/distgo/pkg/git"
    34  	"github.com/palantir/godel/apps/distgo/pkg/git/gittest"
    35  )
    36  
    37  const (
    38  	testMain = `package main
    39  
    40  import "fmt"
    41  
    42  var testVersionVar = "defaultVersion"
    43  
    44  func main() {
    45  	fmt.Println(testVersionVar)
    46  }
    47  `
    48  )
    49  
    50  func TestPublishLocal(t *testing.T) {
    51  	tmp, cleanup, err := dirs.TempDir("", "")
    52  	defer cleanup()
    53  	require.NoError(t, err)
    54  
    55  	for i, currCase := range []struct {
    56  		buildSpec   func(projectDir string) params.ProductBuildSpecWithDeps
    57  		skip        func() bool
    58  		wantPaths   []string
    59  		wantContent map[string]string
    60  	}{
    61  		{
    62  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
    63  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
    64  					Version:  "0.0.1",
    65  					Branch:   "0.0.1",
    66  					Revision: "0",
    67  				}, params.Product{
    68  					Build: params.Build{
    69  						MainPkg: "./.",
    70  					},
    71  					DefaultPublish: params.Publish{
    72  						GroupID: "com.palantir.distgo-publish-test",
    73  					},
    74  				}, params.Project{}), nil)
    75  				require.NoError(t, err)
    76  				return specWithDeps
    77  			},
    78  			wantPaths: []string{
    79  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
    80  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.sls.tgz",
    81  			},
    82  			wantContent: map[string]string{
    83  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n<modelVersion>4.0.0</modelVersion>\n<groupId>com.palantir.distgo-publish-test</groupId>\n<artifactId>publish-test-service</artifactId>\n<version>0.0.1</version>\n<packaging>sls.tgz</packaging>\n</project>\n",
    84  			},
    85  		},
    86  		{
    87  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
    88  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
    89  					Version:  "0.0.1",
    90  					Branch:   "0.0.1",
    91  					Revision: "0",
    92  				}, params.Product{
    93  					Build: params.Build{
    94  						MainPkg: "./.",
    95  					},
    96  					Dist: []params.Dist{{
    97  						Info: &params.BinDistInfo{},
    98  					}},
    99  					DefaultPublish: params.Publish{
   100  						GroupID: "com.palantir.distgo-publish-test",
   101  					},
   102  				}, params.Project{}), nil)
   103  				require.NoError(t, err)
   104  				return specWithDeps
   105  			},
   106  			wantPaths: []string{
   107  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   108  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.tgz",
   109  			},
   110  			wantContent: map[string]string{
   111  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n<modelVersion>4.0.0</modelVersion>\n<groupId>com.palantir.distgo-publish-test</groupId>\n<artifactId>publish-test-service</artifactId>\n<version>0.0.1</version>\n<packaging>tgz</packaging>\n</project>\n",
   112  			},
   113  		},
   114  		{
   115  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   116  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
   117  					Version:  "0.0.1",
   118  					Branch:   "0.0.1",
   119  					Revision: "0",
   120  				}, params.Product{
   121  					Build: params.Build{
   122  						MainPkg: "./.",
   123  					},
   124  					DefaultPublish: params.Publish{
   125  						GroupID: "com.palantir.distgo-publish-test",
   126  					},
   127  				}, params.Project{}), nil)
   128  				require.NoError(t, err)
   129  				return specWithDeps
   130  			},
   131  			wantPaths: []string{
   132  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   133  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.sls.tgz",
   134  			},
   135  			wantContent: map[string]string{
   136  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n<modelVersion>4.0.0</modelVersion>\n<groupId>com.palantir.distgo-publish-test</groupId>\n<artifactId>publish-test-service</artifactId>\n<version>0.0.1</version>\n<packaging>sls.tgz</packaging>\n</project>\n",
   137  			},
   138  		},
   139  		{
   140  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   141  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "test", git.ProjectInfo{
   142  					Version:  "0.0.1",
   143  					Branch:   "0.0.1",
   144  					Revision: "0",
   145  				}, params.Product{
   146  					Build: params.Build{
   147  						MainPkg: "./.",
   148  					},
   149  					Dist: []params.Dist{{
   150  						Info:      &params.BinDistInfo{},
   151  						OutputDir: "dist/bin",
   152  						Script:    "touch $DIST_DIR/dist-1.txt",
   153  					}, {
   154  						Info:      &params.RPMDistInfo{},
   155  						OutputDir: "dist/rpm",
   156  						Script:    "touch $DIST_DIR/dist-2.txt",
   157  						Publish: params.Publish{
   158  							GroupID: "com.palantir.pcloud-rpm",
   159  						},
   160  					}},
   161  					DefaultPublish: params.Publish{
   162  						GroupID: "com.palantir.pcloud-bin",
   163  					},
   164  				}, params.Project{}), nil)
   165  				require.NoError(t, err)
   166  				return specWithDeps
   167  			},
   168  			skip: func() bool {
   169  				// rpm dist type is currently only supported for linux-amd64
   170  				return !(runtime.GOOS == "linux" && runtime.GOARCH == "amd64")
   171  			},
   172  			wantPaths: []string{
   173  				"com/palantir/pcloud-bin/test/0.0.1/test-0.0.1.pom",
   174  				"com/palantir/pcloud-bin/test/0.0.1/test-0.0.1.tgz",
   175  				"com/palantir/pcloud-rpm/test/0.0.1/test-0.0.1.pom",
   176  				"com/palantir/pcloud-rpm/test/0.0.1/test-0.0.1-1.x86_64.rpm",
   177  			},
   178  			wantContent: map[string]string{
   179  				"com/palantir/pcloud-bin/test/0.0.1/test-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n<modelVersion>4.0.0</modelVersion>\n<groupId>com.palantir.pcloud-bin</groupId>\n<artifactId>test</artifactId>\n<version>0.0.1</version>\n<packaging>tgz</packaging>\n</project>\n",
   180  				"com/palantir/pcloud-rpm/test/0.0.1/test-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n<modelVersion>4.0.0</modelVersion>\n<groupId>com.palantir.pcloud-rpm</groupId>\n<artifactId>test</artifactId>\n<version>0.0.1</version>\n<packaging>rpm</packaging>\n</project>\n",
   181  			},
   182  		},
   183  	} {
   184  		if currCase.skip != nil && currCase.skip() {
   185  			fmt.Printf("Skipping case %d\n", i)
   186  			continue
   187  		}
   188  
   189  		currTmp, err := ioutil.TempDir(tmp, "")
   190  		require.NoError(t, err)
   191  
   192  		gittest.InitGitDir(t, currTmp)
   193  
   194  		err = ioutil.WriteFile(path.Join(currTmp, "main.go"), []byte(testMain), 0644)
   195  		require.NoError(t, err)
   196  
   197  		currSpecWithDeps := currCase.buildSpec(currTmp)
   198  
   199  		err = build.Run(build.RequiresBuild(currSpecWithDeps, nil).Specs(), nil, build.Context{}, ioutil.Discard)
   200  		require.NoError(t, err, "Case %d", i)
   201  
   202  		err = dist.Run(currSpecWithDeps, ioutil.Discard)
   203  		require.NoError(t, err, "Case %d", i)
   204  
   205  		repo := path.Join(currTmp, "repository")
   206  		err = publish.Run(currSpecWithDeps, publish.LocalPublishInfo{
   207  			Path: repo,
   208  		}, nil, ioutil.Discard)
   209  		require.NoError(t, err, "Case %d", i)
   210  
   211  		for _, currPath := range currCase.wantPaths {
   212  			info, err := os.Stat(path.Join(repo, currPath))
   213  			require.NoError(t, err, "Case %d", i)
   214  			assert.False(t, info.IsDir(), "Case %d", i)
   215  		}
   216  
   217  		for k, v := range currCase.wantContent {
   218  			bytes, err := ioutil.ReadFile(path.Join(repo, k))
   219  			require.NoError(t, err)
   220  			assert.Equal(t, v, string(bytes), "Case %d", i)
   221  		}
   222  	}
   223  }