Skip to content

Commit

Permalink
chore: return the separate getRootLogMessages and getLogMessages
Browse files Browse the repository at this point in the history
other unrelated tests were depending on the specific behavior.
we can always come back to refactor further.
  • Loading branch information
Walther committed May 31, 2023
1 parent a2b8371 commit 24446c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions core/src/util/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@ export interface EventLogEntry<N extends EventName = any> {
}

/**
* Retrieves all the log entries via the root log and returns a list of all the messages,
* Retrieves the log entries from the given log context and returns a list of all the messages,
* stripped of ANSI characters. Useful to check if a particular message was logged.
*/
export function getLogMessages(log: Log, filter?: (log: LogEntry) => boolean) {
return log
.getAllLogEntries()
.getLogEntries()
.filter((entry) => (filter ? filter(entry) : true))
.map((entry) => stripAnsi(entry.msg || ""))
}

/**
* Retrieves all the entries from the root log and returns a list of all the messages,
* stripped of ANSI characters. Useful to check if a particular message was logged.
*/
export function getRootLogMessages(log: Log, filter?: (log: LogEntry) => boolean) {
return log.getAllLogEntries().filter((entry) => (filter ? filter(entry) : true))
}

type PartialActionConfig = Partial<ActionConfig> & { kind: ActionKind; type: string; name: string }
type PartialModuleConfig = Partial<ModuleConfig> & { name: string; path: string }

Expand Down
6 changes: 3 additions & 3 deletions plugins/terraform/test/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { join } from "path"
import { expect } from "chai"
import { pathExists, readFile, remove } from "fs-extra"

import { getLogMessages, makeTestGarden, TestGarden } from "@garden-io/sdk/testing"
import { getRootLogMessages, makeTestGarden, TestGarden } from "@garden-io/sdk/testing"
import { findByName } from "@garden-io/core/build/src/util/util"
import { getTerraformCommands } from "../commands"
import { ConfigGraph, LogLevel } from "@garden-io/sdk/types"
Expand Down Expand Up @@ -64,7 +64,7 @@ for (const terraformVersion of ["0.13.3", defaultTerraformVersion]) {

it("should warn if stack is not up-to-date", async () => {
const provider = await garden.resolveProvider(garden.log, "terraform")
const messages = getLogMessages(garden.log, (e) => e.level === LogLevel.warn)
const messages = getRootLogMessages(garden.log, (e) => e.level === LogLevel.warn)
expect(messages).to.include(
"Terraform stack is not up-to-date and autoApply is not enabled. Please run garden plugins terraform apply-root to make sure the stack is in the intended state."
)
Expand Down Expand Up @@ -507,7 +507,7 @@ for (const terraformVersion of ["0.13.3", defaultTerraformVersion]) {
context("autoApply=false", () => {
it("should warn if the stack is out of date", async () => {
await deployStack(false)
const messages = getLogMessages(garden.log, (e) => e.level === LogLevel.warn)
const messages = getRootLogMessages(garden.log, (e) => e.level === LogLevel.warn)
expect(messages).to.include(
"Stack is out-of-date but autoApply is set to false, so it will not be applied automatically. If any newly added stack outputs are referenced via ${runtime.services.tf.outputs.*} template strings and are missing, you may see errors when resolving them."
)
Expand Down
2 changes: 1 addition & 1 deletion sdk/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TestGarden, TestGardenOpts } from "@garden-io/core/build/src/util/testi
import { uuidv4 } from "@garden-io/core/build/src/util/random"
import { LogLevel, RootLogger } from "@garden-io/core/build/src/logger/logger"

export { TestGarden, getLogMessages } from "@garden-io/core/build/src/util/testing"
export { TestGarden, getLogMessages, getRootLogMessages } from "@garden-io/core/build/src/util/testing"
export { expectError } from "@garden-io/core/build/src/util/testing"
export { makeTempDir } from "@garden-io/core/build/src/util/fs"

Expand Down

0 comments on commit 24446c1

Please sign in to comment.