From 00b294051fc3d7461f10c725cba5b4ae36657c0e Mon Sep 17 00:00:00 2001 From: softwaredevelop <61334390+softwaredevelop@users.noreply.github.com> Date: Sat, 28 Dec 2024 20:41:16 +0100 Subject: [PATCH] feat: Add CheckWithConfig functions --- yamllint/main.go | 14 ++++++++++++++ yamllint/main_test.go | 5 +---- yamllint/test/main.go | 18 ++++++++++++++++++ yamllint/test/testdata/{ => .config}/.yamllint | 0 4 files changed, 33 insertions(+), 4 deletions(-) rename yamllint/test/testdata/{ => .config}/.yamllint (100%) diff --git a/yamllint/main.go b/yamllint/main.go index 57f5a30..0899e06 100644 --- a/yamllint/main.go +++ b/yamllint/main.go @@ -55,6 +55,20 @@ func (m *Yamllint) Container() *dagger.Container { return m.Ctr } +// CheckWithConfig runs the yamllint command with a configuration file. +func (m *Yamllint) CheckWithConfig( + // source is an optional argument that specifies a directory. + source *dagger.Directory, + // file is an optional argument that specifies yamllint configuration file. + file *dagger.File, +) *dagger.Container { + return m.Container(). + WithMountedDirectory("/tmp", source). + WithWorkdir("/tmp"). + WithFile("/.config/.yamllint", file). + WithExec([]string{"sh", "-c", "find . -type f \\( -name '*.yaml' -o -name '*.yml' \\) -print0 | xargs -0 yamllint -c /.config/.yamllint"}) +} + // Check runs yamllint on the provided source directory. func (m *Yamllint) Check( // source is an optional argument that specifies a directory. diff --git a/yamllint/main_test.go b/yamllint/main_test.go index b6b7d8c..2339f91 100644 --- a/yamllint/main_test.go +++ b/yamllint/main_test.go @@ -37,10 +37,7 @@ func Test_yamllint(t *testing.T) { _, err := container. WithMountedDirectory("/tmp", c.Host().Directory("./test/testdata")). WithWorkdir("/tmp"). - WithExec([]string{"yamllint", - "--config-file", - ".yamllint", - "."}). + WithExec([]string{"sh", "-c", "find . -type f \\( -name '*.yaml' -o -name '*.yml' \\) -print0 | xargs -0 yamllint -c /tmp/.config/.yamllint"}). Stderr(ctx) require.Error(t, err) require.Contains(t, err.Error(), "exit code: 1") diff --git a/yamllint/test/main.go b/yamllint/test/main.go index 1f54f8d..5c63b1d 100644 --- a/yamllint/test/main.go +++ b/yamllint/test/main.go @@ -27,11 +27,29 @@ type Yamllinttest struct{} func (m *Yamllinttest) All(ctx context.Context) error { p := pool.New().WithErrors().WithContext(ctx) + p.Go(m.CheckWithConfig) p.Go(m.Check) return p.Wait() } +// CheckWithConfig runs the yamllint command with a configuration file. +func (m *Yamllinttest) CheckWithConfig(ctx context.Context) error { + + dir := dag.CurrentModule().Source().Directory("./testdata") + file := dag.CurrentModule().Source().File("./testdata/.config/.yamllint") + _, err := dag.Yamllint().CheckWithConfig(dir, file).Stderr(ctx) + + if err != nil { + re := regexp.MustCompile("exit code: 123") + if re.MatchString(err.Error()) { + return nil + } + } + + return err +} + // Check runs the revive command. func (m *Yamllinttest) Check(ctx context.Context) error { diff --git a/yamllint/test/testdata/.yamllint b/yamllint/test/testdata/.config/.yamllint similarity index 100% rename from yamllint/test/testdata/.yamllint rename to yamllint/test/testdata/.config/.yamllint