github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/buildpacks/init_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package buildpacks
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  func TestValidate(t *testing.T) {
    28  	var tests = []struct {
    29  		description   string
    30  		path          string
    31  		otherFiles    []string
    32  		expectedValid bool
    33  	}{
    34  		{
    35  			description:   "NodeJS",
    36  			path:          filepath.Join("path", "to", "package.json"),
    37  			expectedValid: true,
    38  		},
    39  		{
    40  			description:   "NodeJS (root)",
    41  			path:          "package.json",
    42  			expectedValid: true,
    43  		},
    44  		{
    45  			description:   "Go",
    46  			path:          filepath.Join("path", "to", "go.mod"),
    47  			expectedValid: true,
    48  		},
    49  		{
    50  			description:   "Go (root)",
    51  			path:          "go.mod",
    52  			expectedValid: true,
    53  		},
    54  		{
    55  			description: "Python",
    56  			path:        filepath.Join("path", "to", "requirements.txt"),
    57  			otherFiles: []string{
    58  				filepath.Join("path", "to", "Procfile"),
    59  			},
    60  			expectedValid: true,
    61  		},
    62  		{
    63  			description:   "Python missing Procfile",
    64  			path:          filepath.Join("path", "to", "requirements.txt"),
    65  			expectedValid: false,
    66  		},
    67  		{
    68  			description:   "Python (root)",
    69  			path:          "requirements.txt",
    70  			otherFiles:    []string{"Procfile"},
    71  			expectedValid: true,
    72  		},
    73  		{
    74  			description:   "Java Maven",
    75  			path:          filepath.Join("path", "to", "pom.xml"),
    76  			expectedValid: true,
    77  		},
    78  		{
    79  			description:   "Java Gradle",
    80  			path:          filepath.Join("path", "to", "build.gradle"),
    81  			expectedValid: true,
    82  		},
    83  		{
    84  			description:   "Java Gradle Kotlin",
    85  			path:          filepath.Join("path", "to", "build.gradle.kts"),
    86  			expectedValid: true,
    87  		},
    88  		{
    89  			description:   ".NET project",
    90  			path:          "test.csproj",
    91  			expectedValid: true,
    92  		},
    93  		{
    94  			description:   "Buildpacks",
    95  			path:          "project.toml",
    96  			expectedValid: true,
    97  		},
    98  		{
    99  			description:   "Unknown language",
   100  			path:          filepath.Join("path", "to", "something.txt"),
   101  			expectedValid: false,
   102  		},
   103  	}
   104  	for _, test := range tests {
   105  		testutil.Run(t, test.description, func(t *testutil.T) {
   106  			tmpDir := t.NewTempDir().Touch(test.path).Touch(test.otherFiles...)
   107  
   108  			isValid := Validate(tmpDir.Path(test.path))
   109  
   110  			t.CheckDeepEqual(test.expectedValid, isValid)
   111  		})
   112  	}
   113  }
   114  
   115  func TestDescribe(t *testing.T) {
   116  	var tests = []struct {
   117  		description    string
   118  		config         ArtifactConfig
   119  		expectedPrompt string
   120  	}{
   121  		{
   122  			description:    "buildpacks - NodeJS",
   123  			config:         ArtifactConfig{File: "/path/to/package.json"},
   124  			expectedPrompt: "Buildpacks (/path/to/package.json)",
   125  		},
   126  	}
   127  	for _, test := range tests {
   128  		testutil.Run(t, test.description, func(t *testutil.T) {
   129  			t.CheckDeepEqual(test.expectedPrompt, test.config.Describe())
   130  		})
   131  	}
   132  }
   133  
   134  func TestArtifactType(t *testing.T) {
   135  	var tests = []struct {
   136  		description  string
   137  		config       ArtifactConfig
   138  		expectedType latest.ArtifactType
   139  	}{
   140  		{
   141  			description: "buildpacks - NodeJS",
   142  			config: ArtifactConfig{
   143  				File:    filepath.Join("path", "to", "package.json"),
   144  				Builder: "some/builder",
   145  			},
   146  			expectedType: latest.ArtifactType{
   147  				BuildpackArtifact: &latest.BuildpackArtifact{
   148  					Builder: "some/builder",
   149  				},
   150  			},
   151  		},
   152  	}
   153  	for _, test := range tests {
   154  		testutil.Run(t, test.description, func(t *testutil.T) {
   155  			at := test.config.ArtifactType("ignored") // buildpacks doesn't include file references in its artifacts
   156  
   157  			t.CheckDeepEqual(test.expectedType, at)
   158  		})
   159  	}
   160  }