github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/fn/render/cmdrender_test.go (about)

     1  // Copyright 2021 Google LLC
     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 render
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/kpt/internal/printer/fake"
    23  	"github.com/GoogleContainerTools/kpt/internal/testutil"
    24  	"github.com/spf13/cobra"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestCmd_flagAndArgParsing_Symlink(t *testing.T) {
    29  	dir := t.TempDir()
    30  	defer testutil.Chdir(t, dir)()
    31  
    32  	err := os.MkdirAll(filepath.Join(dir, "path", "to", "pkg", "dir"), 0700)
    33  	assert.NoError(t, err)
    34  	err = os.Symlink(filepath.Join("path", "to", "pkg", "dir"), "foo")
    35  	assert.NoError(t, err)
    36  
    37  	// verify the branch ref is set to the correct value
    38  	r := NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
    39  	r.Command.RunE = NoOpRunE
    40  	r.Command.SetArgs([]string{"foo"})
    41  	err = r.Command.Execute()
    42  	assert.NoError(t, err)
    43  	assert.Equal(t, filepath.Join("path", "to", "pkg", "dir"), r.pkgPath)
    44  }
    45  
    46  // NoOpRunE is a noop function to replace the run function of a command.  Useful for testing argument parsing.
    47  var NoOpRunE = func(cmd *cobra.Command, args []string) error { return nil }