-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
acc: add a helper to diff with replacements #2352
Conversation
diff.py is like "diff -r -U2" but it applies replacements first to the argument. This allows comparing different output files and directories but ignore differences that are going to be replaced by placeholders. This is useful for tests that record large amount of files, specifically "bundle init" with standard templates. In those tests, changing one parameter results in a small diff so recording the full directory is not helpful, because it's hard to see what changed there. I'm using it in implementation of serverless mode for templates that need it: #2348 Related small changes: add [TESTROOT] replacement for absolute path to acceptance directory in git repo. Add $TESTDIR env var for absolute path to a given test in git repo.
acceptance/acceptance_test.go
Outdated
@@ -56,6 +56,7 @@ const ( | |||
EntryPointScript = "script" | |||
CleanupScript = "script.cleanup" | |||
PrepareScript = "script.prepare" | |||
ReplsFile = "repls.json" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment what's this file used for? From first glance it's not clear if it's an input for replacement or some sort of output
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment.
@@ -65,6 +66,10 @@ var Scripts = map[string]bool{ | |||
PrepareScript: true, | |||
} | |||
|
|||
var Ignored = map[string]bool{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to make this configurable, maybe in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can discuss it once we have use case for it.
@@ -320,6 +333,10 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont | |||
cmd.Env = append(cmd.Env, "GOCOVERDIR="+coverDir) | |||
} | |||
|
|||
absDir, err := filepath.Abs(dir) | |||
require.NoError(t, err) | |||
cmd.Env = append(cmd.Env, "TESTDIR="+absDir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't absDir
value be in quotes? It might contain spaces at least on Windows
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This passes on Windows CI and it's also not the only directory we have, so judging from practice, no. Unless you have a Windows machine where it does not work without quotes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as it passes on Windows CI it's fine I guess
p1 = d1 / f | ||
p2 = d2 / f | ||
if f not in set2: | ||
print(f"Only in {d1}: {f}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make it a bit more explicit phrasing? Something like "File X is found only in Y directory". When I was reading test output it was not immediately clear what's going on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This follows "diff -r" phrasing. If you're familiar with that, it makes sense.
acceptance/config_test.go
Outdated
@@ -27,6 +27,9 @@ type TestConfig struct { | |||
// If true, do not run this test against cloud environment | |||
LocalOnly bool | |||
|
|||
// if true, save file repls.json with all the replacemnts | |||
SaveRepls bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we instead of saving and then loading replacements just do the output replacement always (maybe to a separate temp folder) and then compare it? Or just always save it and ignore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather avoid adding this overhead to all tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, File i/o is cheap so this should not add much overhead? Doing replacement on all output files by default makes sense since we do that anyways when performing comparisons. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also get to avoid the added complexity of having SaveRepls
and repls.json
.
And we could use diff
directly instead of relying on the python script to perform replacements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denik by overhead you mean performance overhead? If so, how do we balance between performance and code complexity then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we could use diff directly instead of relying on the python script to perform replacements.
I don't see how that is possible, the replacements are applied by test runner but on file system the files are without replacements so diff will see the actual values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the files are without replacements so diff will see the actual values
I was proposing we change this. If the files on the file system are with the replacement then we could use diff directly.
But on thinking about this more it would make debugging harder since since you don't know the pre-replacement values. The python approach makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shreyas-goenka I don't think this is possible as the replacements could be done only after script
is finished and diff
call is a part of the script
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively we could introduce some sort of post script
run validation script which would call the diff
and compare expected output but it might be more complex
acceptance/acceptance_test.go
Outdated
if config.SaveRepls { | ||
replsJson, err := json.MarshalIndent(repls.Repls, "", " ") | ||
require.NoError(t, err) | ||
testutil.WriteFile(t, filepath.Join(tmpDir, ReplsFile), string(replsJson)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be unlikely to happen but if my test script also produces repls.json this will override it, correct? Shall we error out in this case just to be safe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is written before your script is run, so your script will override it, which will break diff.py if you use it.
You should be able to modify your script to use different file or rename the files like we do with .gitignore.
@@ -0,0 +1,56 @@ | |||
#!/usr/bin/env python3 | |||
"""This script implements "diff -r -U2 dir1 dir2" but applies replacements first""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you have a chance to measure performance difference between running this test with diff
and with repls.json writing + diff.py?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand -- diff
is doing the wrong thing there, e.g. it will look at actual username rather than [USERNAME] and detect that as a difference. So performance is not important because they are not equivalent, functionality-wise.
We can bench diff.py against diff but even if it's 2x slower there is no action to take, because we use diff.py for handling replacements.
That's said I don't think diffing will be a bottleneck in your tests, so it makes sense to prefer diff.py
because it has less cross-platform quirks than diff
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This relates to discussion in the other thread about potentially replacing the output without storing repl.json file and using diff
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But since it's not really possible to use diff
anyway, performance here indeed doesn't really matter
Changes
diff.py is like "diff -r -U2" but it applies replacements first to the argument.
This allows comparing different output files and directories but ignore differences that are going to be replaced by placeholders.
This is useful for tests that record large amount of files, specifically "bundle init" with standard templates. In those tests, changing one parameter results in a small diff so recording the full directory is not helpful, because it's hard to see what changed there. I'm using it in implementation of serverless mode for templates that need it: #2348 The serverless templates are slightly different from classic, capturing the diff helps to see exactly where.
Related small changes:
Tests