Skip to content

feat: Add diff mode#112

Open
maratori wants to merge 1 commit intogoogle:mainfrom
maratori:diff
Open

feat: Add diff mode#112
maratori wants to merge 1 commit intogoogle:mainfrom
maratori:diff

Conversation

@maratori
Copy link

@maratori maratori commented Jan 9, 2026

This PR adds new --mode diff and closes #111

Example:

--- goldens/separator.in
+++ goldens/separator.in
@@ -1,17 +1,17 @@
 Comma separator:
 static class Foo {
   // keep-sorted-test start
-  "Foo",
+  "Bar",
-  "Bar",
+  "Baz",
-  "Baz"
+  "Foo"
   // keep-sorted-test end
 }
 
 Comma separator with comments:
-// keep-sorted-test start sticky_comments=yes
-"Foo",
+// keep-sorted-test start sticky_comments=yes
 // Sticky comment
 "Bar",
-"Baz"
+"Baz",
+"Foo"
 // Trailing comments
 // keep-sorted-test end

@JeffFaer JeffFaer self-requested a review February 22, 2026 05:51
}
}

for _, warn := range warnings {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract a logWarnings function to deduplicate this logic with fix

}

gotOut2, _, exitCode2, err := runKeepSorted(strings.NewReader(gotOut))
_, err = in.Seek(0, 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just read in once and use bytes.NewReader when we run keep-sorted

if err != nil {
t.Errorf("Had trouble running keep-sorted --mode diff: %v", err)
}
if diff := cmp.Diff(string(wantDiff), gotDiff); diff != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I see a whole lot of value in writing down both the out and diff files. They should theoretically be derivable from each other

What do you think about instead of doing the comparison like this, we instead apply the output of the diff operation to the input and make sure we get the out file?

"strconv"
"strings"

"github.com/aymanbagabas/go-udiff"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: I need to make sure this package is available internally. It probably is, but just leaving a note for myself next week

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: output diff

2 participants