Skip to content

Commit

Permalink
add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao committed Jan 7, 2025
1 parent 05b2a8f commit e5dff7a
Show file tree
Hide file tree
Showing 2 changed files with 466 additions and 19 deletions.
44 changes: 28 additions & 16 deletions internal/controller/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func (d *managedData) hasLint() bool {
if env == nil {
return false
}
return env.Body().GetAttribute("lint") != nil
return searchBlock(env.Body(), hclwrite.NewBlock("lint", nil)) != nil
}

// hasLintDestructive returns true if the environment has a lint destructive policy.
Expand Down Expand Up @@ -813,30 +813,42 @@ func (d *managedData) render(w io.Writer) error {
// enableDestructive enables the linting policy for destructive changes.
// If the force is set to true, it will override the existing value.
func (d *managedData) enableDestructive(force bool) {
check := &dbv1alpha1.CheckConfig{Error: true}
destructive := &dbv1alpha1.Lint{Destructive: check}
switch {
case d.Policy == nil && !d.hasLint():
d.Policy = &dbv1alpha1.Policy{Lint: destructive}
case !d.Policy.HasLint() && !d.hasLint():
d.Policy.Lint = destructive
case !d.Policy.HasLintDestructive() && !d.hasLintDestructive(), force:
enable := func() {
check := &dbv1alpha1.CheckConfig{Error: true}
destructive := &dbv1alpha1.Lint{Destructive: check}
if d.Policy == nil {
d.Policy = &dbv1alpha1.Policy{Lint: destructive}
return
}
if d.Policy.Lint == nil {
d.Policy.Lint = destructive
return
}
d.Policy.Lint.Destructive = check
}
if !d.hasLint() || !d.hasLintDestructive() || force {
enable()
}
}

// setLintReview sets the lint review policy.
// If the force is set to true, it will override the existing value.
func (d *managedData) setLintReview(v dbv1alpha1.LintReview, force bool) {
lint := &dbv1alpha1.Lint{Review: v}
switch {
case d.Policy == nil && !d.hasLint():
d.Policy = &dbv1alpha1.Policy{Lint: lint}
case !d.Policy.HasLint() && !d.hasLint():
d.Policy.Lint = lint
case !d.Policy.HasLintReview() && !d.hasLintReview(), force:
enable := func() {
lint := &dbv1alpha1.Lint{Review: v}
if d.Policy == nil {
d.Policy = &dbv1alpha1.Policy{Lint: lint}
return
}
if d.Policy.Lint == nil {
d.Policy.Lint = lint
return
}
d.Policy.Lint.Review = v
}
if !d.hasLint() || !d.hasLintReview() || force {
enable()
}
}

// asBlocks returns the HCL block for the environment configuration.
Expand Down
Loading

0 comments on commit e5dff7a

Please sign in to comment.