Skip to content

Only replace @CsvSource values that CSV parsing leaves unchanged in CsvSourceToValueSource - #1244

Merged
timtebeek merged 3 commits into
mainfrom
tim/c8-fix
Oct 25, 2026
Merged

timtebeek merged 3 commits into
mainfrom
tim/c8-fix

Conversation

@timtebeek

Copy link
Copy Markdown
Member

CsvSourceToValueSource only checked the values of @CsvSource(textBlock = ...). For the value attribute it copied the strings into @ValueSource as they were, although JUnit's CSV parsing changes them. @CsvSource({"x, y", "z, w"}) void t(String s) passed "x" and "z", but after the conversion it passed "x, y" and "z, w". A probe against the JUnit on this project's test classpath showed the parser also strips single quotes, trims surrounding whitespace, rejects blank records, and splits a value with line breaks into several records. For example, the multi-line text block passed as value in the old preserveTextBlockAsValue test ran three times, with # Lower-case letters, asdf and a, not once with the whole string.

Both paths now share one check. They skip the conversion unless every value is a string literal without a comma, single quote or line break, is not blank, and has no surrounding whitespace. Non-String parameters also reject inner whitespace. Non-literal values such as constants are left alone. The old text-block test now expects no change.

The values are now read from the literals directly instead of through the Literal trait, which does not resolve Kotlin array attributes. Because of that:

  • Kotlin non-String parameters are now converted (@ValueSource(ints = [1, 2, 3])) where they were left unchanged before.
  • A single Kotlin value becomes strings = ["apple"] instead of strings = "apple", which does not compile.

Verified with ./gradlew test --offline --tests '*CsvSourceToValueSourceTest', which passed.

Fixes item C8 of #1211.

… `CsvSourceToValueSource`

The `value` path skipped the checks the `textBlock` path made, so `@CsvSource({"x, y", "z, w"}) void t(String s)` became `@ValueSource(strings = {"x, y", "z, w"})` and passed `"x, y"` instead of `"x"`. Quoted values such as `"'a'"`, surrounding whitespace, blank values and values spanning several lines were changed too. Both paths now skip the conversion unless every value is a literal that CSV parsing passes through unchanged.

Reading the values from the literals rather than the `Literal` trait also covers Kotlin array attributes, so Kotlin non-`String` parameters are now converted, and a single Kotlin value becomes `["apple"]` instead of the non-compiling `strings = "apple"`.
@timtebeek
timtebeek enabled auto-merge (squash) October 25, 2026 22:24
@timtebeek
timtebeek merged commit 212fb5e into main Oct 25, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/c8-fix branch October 25, 2026 22:34
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant