Skip to content

Improve tests with inline failure messages #3035

Closed

Description

@ZevEisenberg

New Issue Checklist

Describe the bug

When developing rules and fixing bugs, trying to correlate the test output with the code can be difficult. It would be nicer if incorrectly triggered or ignored examples were highlighted in Xcode like other test failures.

This would be possible with the technique I describe in this blog post: XCTest Line by Line

As a quick sketch, it would transform the test case declarations from this:

public static let description = RuleDescription(
    identifier: "foo",
    name: "Foo",
    description: "some description",
    kind: .idiomatic,
    minSwiftVersion: .fourDotOne,
    nonTriggeringExamples: [
        """
        enum SomeExample { }
        """, // etc

to something more like this:

public static let description = RuleDescription(
    identifier: "foo",
    name: "Foo",
    description: "some description",
    kind: .idiomatic,
    minSwiftVersion: .fourDotOne,
    nonTriggeringExamples: [
        Example("""
        enum SomeExample {}
        """), // etc etc

The addition of the Example wrapper struct would capture the file and line number, which would be passed to the XCTest methods, ensuring that failures are reflected on the correct line. The initializer of the Example struct would have to look like this:

// "example" parameter could also be a StaticString if you want to encourage
// passing string literals to aid in debugging
init(_ example: String, file: StaticString = #file, line: Uint = #line)

I'd be interested in contributing a PR to address this.

Activity

  1. jpsim commented on Jan 13, 2020

    @jpsim
    Collaborator

    We actually already have this, although it might have stopped working a while back.

  2. ZevEisenberg commented on Jan 13, 2020

    @ZevEisenberg
    ContributorAuthor

    Ohh, interesting. Do you know where it is/was? I can look into fixing it up. It would be a real quality-of-life improvement for people doing small fixes who aren't as familiar with the code base as a whole.

  3. jpsim commented on Jan 13, 2020

    @jpsim
    Collaborator

    Turns out we do this for the integration tests: https://github.com/realm/SwiftLint/blob/0.38.2/Tests/SwiftLintFrameworkTests/IntegrationTests.swift#L26-L28

    But we don't do this for the rule examples. This would be a lovely addition.

  4. added
    enhancement
    on Jan 13, 2020
  5. ZevEisenberg commented on Jan 13, 2020

    @ZevEisenberg
    ContributorAuthor

    I'm already working on it, but it turns out there are a ton of rules with weirdly formatted strings, which makes some of this a little tricky. Thoughts on converting them all to multiline strings as part of the conversion effort? I'm pretty sure that either way, this will wreak havoc on any open PRs that substantially change any examples :\

    I had a cool idea of using ExpressibleByStringLiteral to provide a compatibility mode for contributors, which would fail the tests (instead of failing the build) until they've wrapped their examples in Example(...), but that doesn't work for the hundreds or thousands of tests that use old-style "foo\n" + "bar" string concatenations.

    That's why I'm currently investigating SwiftRewriter to see if it's worth building some kind of utility to do the conversion to multiline strings, but the answer is almost definitely "it is not worth it."

  6. ZevEisenberg commented on Jan 14, 2020

    @ZevEisenberg
    ContributorAuthor

    Do you know why so many triggering/non-triggering examples in SwiftLint have a \n at the end of them? Is it required? Wondering if I should clean those up as part of this work. It would certainly clear up some visual noise in the test strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions