Skip to content

Commit

Permalink
Fix custom fmt test snapshots (#256)
Browse files Browse the repository at this point in the history
`customLinterFmtTest`s were written originally to only have one path to
snapshot, and they didn't interact nicely with other format tests. Fix
the regex and the semantics to support multiple snapshots. This was
breaking [`main`
tests](https://github.com/trunk-io/plugins/actions/runs/4730053093/jobs/8404731644)
and is a plugins release blocker.

If need be, it should be straightforward to refine this in the future
(as well as for `customLinterCheckTest`).
  • Loading branch information
TylerJang27 authored Apr 18, 2023
1 parent f6ffcc3 commit 60112d2
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 162 deletions.
20 changes: 15 additions & 5 deletions linters/buildifier/buildifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import { customLinterFmtTest, linterCheckTest, linterFmtTest } from "tests";
import { customLinterCheckTest, customLinterFmtTest } from "tests";
import { TrunkDriver } from "tests/driver";
import { TEST_DATA } from "tests/utils";

Expand Down Expand Up @@ -35,12 +35,22 @@ const writeConfigs = (driver: TrunkDriver) => {
};

// TODO(Tyler): We will eventually need to add a couple more test cases involving failure modes.
linterCheckTest({ linterName: "buildifier", namedTestPrefixes: ["basic", "add_tables"] });
linterFmtTest({ linterName: "buildifier", namedTestPrefixes: ["basic", "add_tables"] });
customLinterCheckTest({
linterName: "buildifier",
args: "-a",
testName: "basic_check",
});

customLinterFmtTest({
linterName: "buildifier",
args: "-a",
testName: "no_config",
pathsToSnapshot: [path.join(TEST_DATA, "basic.bzl"), path.join(TEST_DATA, "add_tables.BUILD")],
});
customLinterFmtTest({
linterName: "buildifier",
args: "-a",
testName: "withConfig",
pathsToSnapshot: [path.join(TEST_DATA, "add_tables.in.BUILD")],
testName: "with_config",
pathsToSnapshot: [path.join(TEST_DATA, "add_tables.BUILD")],
preCheck: writeConfigs,
});
File renamed without changes.

This file was deleted.

69 changes: 0 additions & 69 deletions linters/buildifier/test_data/buildifier_v6.1.0_add_tables.fmt.shot

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter buildifier test basic 1`] = `
exports[`Testing linter buildifier test basic_check 1`] = `
{
"issues": [
{
"code": "module-docstring",
"column": "1",
"file": "test_data/basic.in.bzl",
"file": "test_data/basic.bzl",
"issueUrl": "https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#module-docstring",
"level": "LEVEL_HIGH",
"line": "1",
Expand All @@ -18,7 +18,7 @@ A module docstring is a string literal (not a comment) which should be the first
{
"code": "load",
"column": "26",
"file": "test_data/basic.in.bzl",
"file": "test_data/basic.bzl",
"issueUrl": "https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#load",
"level": "LEVEL_HIGH",
"line": "1",
Expand All @@ -34,7 +34,7 @@ If you want to re-export a symbol, use the following pattern:
{
"code": "out-of-order-load",
"column": "1",
"file": "test_data/basic.in.bzl",
"file": "test_data/basic.bzl",
"issueUrl": "https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#out-of-order-load",
"level": "LEVEL_HIGH",
"line": "2",
Expand All @@ -45,7 +45,7 @@ If you want to re-export a symbol, use the following pattern:
{
"code": "load",
"column": "26",
"file": "test_data/basic.in.bzl",
"file": "test_data/basic.bzl",
"issueUrl": "https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#load",
"level": "LEVEL_HIGH",
"line": "2",
Expand All @@ -60,21 +60,39 @@ If you want to re-export a symbol, use the following pattern:
},
],
"lintActions": [
{
"command": "fix",
"fileGroupName": "bazel-build",
"linter": "buildifier",
"paths": [
"test_data/add_tables.BUILD",
],
"verb": "TRUNK_VERB_FMT",
},
{
"command": "fix",
"fileGroupName": "starlark",
"linter": "buildifier",
"paths": [
"test_data/basic.in.bzl",
"test_data/basic.bzl",
],
"verb": "TRUNK_VERB_FMT",
},
{
"command": "warn",
"fileGroupName": "bazel-build",
"linter": "buildifier",
"paths": [
"test_data/add_tables.BUILD",
],
"verb": "TRUNK_VERB_CHECK",
},
{
"command": "warn",
"fileGroupName": "starlark",
"linter": "buildifier",
"paths": [
"test_data/basic.in.bzl",
"test_data/basic.bzl",
],
"verb": "TRUNK_VERB_CHECK",
},
Expand All @@ -83,7 +101,16 @@ If you want to re-export a symbol, use the following pattern:
"unformattedFiles": [
{
"column": "1",
"file": "test_data/basic.in.bzl",
"file": "test_data/add_tables.BUILD",
"issueClass": "ISSUE_CLASS_UNFORMATTED",
"level": "LEVEL_HIGH",
"line": "1",
"linter": "buildifier",
"message": "Incorrect formatting, autoformat by running 'trunk fmt'",
},
{
"column": "1",
"file": "test_data/basic.bzl",
"issueClass": "ISSUE_CLASS_UNFORMATTED",
"level": "LEVEL_HIGH",
"line": "1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter buildifier test basic 1`] = `
"# Misformatted file
def eponymous_name():
name = native.package_name()

return name[name.rfind("/") + 1:]
"
`;

exports[`Testing formatter buildifier test withConfig 1`] = `
exports[`Testing formatter buildifier test no_config 1`] = `
"foo_macro(
fizz = [
":lib1",
":lib2",
":lib1",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter buildifier test no_config 1`] = `
"# Misformatted file
def eponymous_name():
name = native.package_name()

return name[name.rfind("/") + 1:]
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter buildifier test with_config 1`] = `
"foo_macro(
fizz = [
":lib1",
":lib2",
],
)

filegroup(
name = "files",
srcs = glob(["**"]),
)

sh_library(
name = "lib1",
srcs = ["src1.sh"],
)

sh_library(
name = "lib2",
srcs = ["src1.sh"],
)

sh_binary(
name = "foo",
srcs = ["foo.sh"],
deps = [
":lib1",
":lib2",
],
)
"
`;
2 changes: 2 additions & 0 deletions linters/rubocop/rubocop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ customLinterCheckTest({
args: "-a",
skipTestIf: skipOS(["darwin"]),
});

customLinterFmtTest({
linterName: "rubocop",
testName: "basic",
args: "-a",
pathsToSnapshot: [path.join(TEST_DATA, "basic.rb")],
skipTestIf: skipOS(["darwin"]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter rubocop test CUSTOM 1`] = `
exports[`Testing formatter rubocop test basic 1`] = `
"class Plumbus
private

Expand Down
1 change: 1 addition & 0 deletions linters/rufo/rufo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ customLinterCheckTest({

customLinterFmtTest({
linterName: "rufo",
testName: "basic",
args: "-a",
pathsToSnapshot: [path.join(TEST_DATA, "basic.rb")],
skipTestIf: skipOS(["darwin"]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter rufo test CUSTOM 1`] = `
exports[`Testing formatter rufo test basic 1`] = `
"class Plumbus
private

Expand Down
1 change: 1 addition & 0 deletions linters/sort-package-json/sort_package_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TEST_DATA } from "tests/utils";

customLinterFmtTest({
linterName: "sort-package-json",
testName: "basic",
args: "--all",
pathsToSnapshot: [path.join(TEST_DATA, "package.json")],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter sort-package-json test CUSTOM 1`] = `
exports[`Testing formatter sort-package-json test basic 1`] = `
"{
"private": true,
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions linters/swiftformat/swiftformat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { skipOS, TEST_DATA } from "tests/utils";

customLinterFmtTest({
linterName: "swiftformat",
testName: "basic",
args: "-a",
pathsToSnapshot: [path.join(TEST_DATA, "basic.swift")],
skipTestIf: skipOS(["linux"]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter swiftformat test CUSTOM 1`] = `
exports[`Testing formatter swiftformat test basic 1`] = `
"// Swift "Hello, World!" Program

print("Hello, World!")
Expand Down
Loading

0 comments on commit 60112d2

Please sign in to comment.