Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions hadolint/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"context"
"dagger/hadolint/test/internal/dagger"
"errors"
"strings"

Expand All @@ -16,40 +17,60 @@ type Hadolinttest struct{}
func (m *Hadolinttest) All(ctx context.Context) error {
p := pool.New().WithErrors().WithContext(ctx)

p.Go(m.CheckWithConfig)
p.Go(m.Check)
p.Go(m.CheckWithOptionalConfig)
p.Go(m.CheckWithConfig)

return p.Wait()
}

// CheckWithConfig tests hadolint with an explicit configuration file.
func (m *Hadolinttest) CheckWithConfig(ctx context.Context) error {
// Check tests hadolint on invalid testdata without configuration.
func (m *Hadolinttest) Check(ctx context.Context) error {
dir := dag.CurrentModule().Source().Directory("./testdata")

_, err := dag.Hadolint().Check(dir).Stderr(ctx)
if err == nil {
return errors.New("expected hadolint to fail on invalid Dockerfile, but it succeeded")
}

if strings.Contains(err.Error(), "exit code: 123") || strings.Contains(err.Error(), "exit code: 1") {
return nil
}

return err
}

// CheckWithOptionalConfig tests the new optional config parameter of the Check function.
func (m *Hadolinttest) CheckWithOptionalConfig(ctx context.Context) error {
dir := dag.CurrentModule().Source().Directory("./testdata")
file := dag.CurrentModule().Source().File("./testdata/.config/.hadolint.yaml")

_, err := dag.Hadolint().CheckWithConfig(dir, file).Stderr(ctx)
// Calling Check with the newly introduced optional argument
_, err := dag.Hadolint().Check(dir, dagger.HadolintCheckOpts{
Config: file,
}).Stderr(ctx)

if err == nil {
return errors.New("expected hadolint to fail on invalid Dockerfile, but it succeeded")
}

// xargs exits with 123 when the underlying linter fails
if strings.Contains(err.Error(), "exit code: 123") || strings.Contains(err.Error(), "exit code: 1") {
return nil
}

return err
}

// Check tests hadolint on invalid testdata without explicit configuration.
func (m *Hadolinttest) Check(ctx context.Context) error {
// CheckWithConfig tests the backwards-compatible CheckWithConfig wrapper function.
func (m *Hadolinttest) CheckWithConfig(ctx context.Context) error {
dir := dag.CurrentModule().Source().Directory("./testdata")
file := dag.CurrentModule().Source().File("./testdata/.config/.hadolint.yaml")

_, err := dag.Hadolint().Check(dir).Stderr(ctx)
_, err := dag.Hadolint().CheckWithConfig(dir, file).Stderr(ctx)
if err == nil {
return errors.New("expected hadolint to fail on invalid Dockerfile, but it succeeded")
}

// xargs exits with 123 when the underlying linter fails
if strings.Contains(err.Error(), "exit code: 123") || strings.Contains(err.Error(), "exit code: 1") {
return nil
}
Expand Down
Loading