github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/cli/create_filewatch_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"k8s.io/apimachinery/pkg/types"
    12  	"k8s.io/cli-runtime/pkg/genericclioptions"
    13  
    14  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    15  )
    16  
    17  func TestCreateFileWatch(t *testing.T) {
    18  	f := newServerFixture(t)
    19  
    20  	out := bytes.NewBuffer(nil)
    21  	streams := genericclioptions.IOStreams{Out: out}
    22  
    23  	cmd := newCreateFileWatchCmd(streams)
    24  	c := cmd.register()
    25  	err := c.Flags().Parse([]string{
    26  		"--ignore", "web/node_modules",
    27  		"my-watch", "src", "web",
    28  	})
    29  	require.NoError(t, err)
    30  
    31  	err = cmd.run(f.ctx, c.Flags().Args())
    32  	require.NoError(t, err)
    33  	assert.Contains(t, out.String(), `filewatch.tilt.dev/my-watch created`)
    34  
    35  	var fw v1alpha1.FileWatch
    36  	err = f.client.Get(f.ctx, types.NamespacedName{Name: "my-watch"}, &fw)
    37  	require.NoError(t, err)
    38  
    39  	cwd, _ := os.Getwd()
    40  	assert.Equal(t, []string{
    41  		filepath.Join(cwd, "src"),
    42  		filepath.Join(cwd, "web"),
    43  	}, fw.Spec.WatchedPaths)
    44  	assert.Equal(t, cwd, fw.Spec.Ignores[0].BasePath)
    45  	assert.Equal(t, []string{"web/node_modules"}, fw.Spec.Ignores[0].Patterns)
    46  }
    47  
    48  func TestCreateFileWatchNoIgnore(t *testing.T) {
    49  	f := newServerFixture(t)
    50  
    51  	out := bytes.NewBuffer(nil)
    52  	streams := genericclioptions.IOStreams{Out: out}
    53  
    54  	cmd := newCreateFileWatchCmd(streams)
    55  	c := cmd.register()
    56  	err := c.Flags().Parse([]string{"my-watch", "src"})
    57  	require.NoError(t, err)
    58  
    59  	err = cmd.run(f.ctx, c.Flags().Args())
    60  	require.NoError(t, err)
    61  	assert.Contains(t, out.String(), `filewatch.tilt.dev/my-watch created`)
    62  
    63  	var fw v1alpha1.FileWatch
    64  	err = f.client.Get(f.ctx, types.NamespacedName{Name: "my-watch"}, &fw)
    65  	require.NoError(t, err)
    66  
    67  	cwd, _ := os.Getwd()
    68  	assert.Equal(t, []string{
    69  		filepath.Join(cwd, "src"),
    70  	}, fw.Spec.WatchedPaths)
    71  	assert.Equal(t, 0, len(fw.Spec.Ignores))
    72  }