github.com/databricks/cli@v0.203.0/bundle/config/mutator/process_root_includes_test.go (about)

     1  package mutator_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"path/filepath"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"github.com/databricks/cli/bundle"
    13  	"github.com/databricks/cli/bundle/config"
    14  	"github.com/databricks/cli/bundle/config/mutator"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func touch(t *testing.T, path, file string) {
    20  	f, err := os.Create(filepath.Join(path, file))
    21  	require.NoError(t, err)
    22  	f.Close()
    23  }
    24  
    25  func TestProcessRootIncludesEmpty(t *testing.T) {
    26  	bundle := &bundle.Bundle{
    27  		Config: config.Root{
    28  			Path: ".",
    29  		},
    30  	}
    31  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
    32  	require.NoError(t, err)
    33  }
    34  
    35  func TestProcessRootIncludesAbs(t *testing.T) {
    36  	// remove this once equivalent tests for windows have been set up
    37  	// or this test has been fixed for windows
    38  	// date: 28 Nov 2022
    39  	if runtime.GOOS == "windows" {
    40  		t.Skip("skipping temperorilty to make windows unit tests green")
    41  	}
    42  
    43  	bundle := &bundle.Bundle{
    44  		Config: config.Root{
    45  			Path: ".",
    46  			Include: []string{
    47  				"/tmp/*.yml",
    48  			},
    49  		},
    50  	}
    51  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
    52  	require.Error(t, err)
    53  	assert.Contains(t, err.Error(), "must be relative paths")
    54  }
    55  
    56  func TestProcessRootIncludesSingleGlob(t *testing.T) {
    57  	bundle := &bundle.Bundle{
    58  		Config: config.Root{
    59  			Path: t.TempDir(),
    60  			Include: []string{
    61  				"*.yml",
    62  			},
    63  		},
    64  	}
    65  
    66  	touch(t, bundle.Config.Path, "databricks.yml")
    67  	touch(t, bundle.Config.Path, "a.yml")
    68  	touch(t, bundle.Config.Path, "b.yml")
    69  
    70  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
    71  	require.NoError(t, err)
    72  
    73  	assert.Equal(t, []string{"a.yml", "b.yml"}, bundle.Config.Include)
    74  }
    75  
    76  func TestProcessRootIncludesMultiGlob(t *testing.T) {
    77  	bundle := &bundle.Bundle{
    78  		Config: config.Root{
    79  			Path: t.TempDir(),
    80  			Include: []string{
    81  				"a*.yml",
    82  				"b*.yml",
    83  			},
    84  		},
    85  	}
    86  
    87  	touch(t, bundle.Config.Path, "a1.yml")
    88  	touch(t, bundle.Config.Path, "b1.yml")
    89  
    90  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
    91  	require.NoError(t, err)
    92  
    93  	assert.Equal(t, []string{"a1.yml", "b1.yml"}, bundle.Config.Include)
    94  }
    95  
    96  func TestProcessRootIncludesRemoveDups(t *testing.T) {
    97  	bundle := &bundle.Bundle{
    98  		Config: config.Root{
    99  			Path: t.TempDir(),
   100  			Include: []string{
   101  				"*.yml",
   102  				"*.yml",
   103  			},
   104  		},
   105  	}
   106  
   107  	touch(t, bundle.Config.Path, "a.yml")
   108  
   109  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
   110  	require.NoError(t, err)
   111  	assert.Equal(t, []string{"a.yml"}, bundle.Config.Include)
   112  }
   113  
   114  func TestProcessRootIncludesNotExists(t *testing.T) {
   115  	bundle := &bundle.Bundle{
   116  		Config: config.Root{
   117  			Path: t.TempDir(),
   118  			Include: []string{
   119  				"notexist.yml",
   120  			},
   121  		},
   122  	}
   123  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
   124  	require.Error(t, err)
   125  	assert.Contains(t, err.Error(), "notexist.yml defined in 'include' section does not match any files")
   126  }
   127  
   128  func TestProcessRootIncludesExtrasFromEnvVar(t *testing.T) {
   129  	rootPath := t.TempDir()
   130  	testYamlName := "extra_include_path.yml"
   131  	touch(t, rootPath, testYamlName)
   132  	os.Setenv(bundle.ExtraIncludePathsKey, path.Join(rootPath, testYamlName))
   133  	t.Cleanup(func() {
   134  		os.Unsetenv(bundle.ExtraIncludePathsKey)
   135  	})
   136  
   137  	bundle := &bundle.Bundle{
   138  		Config: config.Root{
   139  			Path: rootPath,
   140  		},
   141  	}
   142  
   143  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
   144  	require.NoError(t, err)
   145  	assert.Contains(t, bundle.Config.Include, testYamlName)
   146  }
   147  
   148  func TestProcessRootIncludesDedupExtrasFromEnvVar(t *testing.T) {
   149  	rootPath := t.TempDir()
   150  	testYamlName := "extra_include_path.yml"
   151  	touch(t, rootPath, testYamlName)
   152  	t.Setenv(bundle.ExtraIncludePathsKey, fmt.Sprintf("%s%s%s", path.Join(rootPath, testYamlName), string(os.PathListSeparator), path.Join(rootPath, testYamlName)))
   153  
   154  	bundle := &bundle.Bundle{
   155  		Config: config.Root{
   156  			Path: rootPath,
   157  		},
   158  	}
   159  
   160  	err := mutator.ProcessRootIncludes().Apply(context.Background(), bundle)
   161  	require.NoError(t, err)
   162  	assert.Equal(t, []string{testYamlName}, bundle.Config.Include)
   163  }