Skip to content
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

Issue #6232: Load resources relative to resources root #6600

Merged
merged 1 commit into from
Jun 7, 2019

Conversation

peterdemaeyer
Copy link
Contributor

@peterdemaeyer peterdemaeyer commented Mar 21, 2019

Issue #6232

  1. Added new unit tests to illustrate problem - test fails.
  2. Fixed code - test succeeds.
  3. Adapted existing unit test for new behavior.
    "mvn clean verify" succeeded.

@rnveach
Copy link
Member

rnveach commented Mar 21, 2019

@peterdemaeyer spellchecker in travis is failed and pitest is failing in circle ci.
Please keep number of commits at 1 while you make CI pass.

@rnveach
Copy link
Member

rnveach commented Mar 27, 2019

@peterdemaeyer pitest is still failing.

@peterdemaeyer
Copy link
Contributor Author

The build timed out while downloading dependencies. Can someone please rebuild to see if it's maybe a transient issue? Apparently I don't have permission to trigger a rebuild myself.

@rnveach
Copy link
Member

rnveach commented Mar 28, 2019

I restarted the CI.

@peterdemaeyer
Copy link
Contributor Author

Thanks, all green now.

@rnveach rnveach self-assigned this Apr 1, 2019
@romani
Copy link
Member

romani commented Apr 17, 2019

@peterdemaeyer , thanks a lot for your updates.
FYI, if no activity till end of month we will close PR (just to keep out list of PRs manageable), you are welcome to reopen it ones you have new updates and ready to continue.

@peterdemaeyer
Copy link
Contributor Author

Fixed the review comments, but now the wercker/build fails with an unclear "401". mvn clean verify succeeds locally.

@romani
Copy link
Member

romani commented Apr 27, 2019

@rnveach , please continue/finish review.

@rnveach
Copy link
Member

rnveach commented Apr 27, 2019

@peterdemaeyer failure is because your branch is based on an outdated master. Please rebase.

@@ -0,0 +1 @@
good
Copy link
Member

Choose a reason for hiding this comment

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

Why does this file need to be in its own folder and not part of checkstyle's standard packaging?

Copy link
Contributor Author

@peterdemaeyer peterdemaeyer Apr 27, 2019

Choose a reason for hiding this comment

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

Because it most elegantly illustrates the initial issue I encountered and have now fixed - which is to reach for a file outside of the package you're currently in.
I could move everything inside the "standard packaging", but at the expense of adding an excessively deep directory tree.
The "good" file would be then in src/test/resources/com/puppycrawl/tools/checkstyle/utils/commonutil, the "bad" file would be in src/test/resources/com/puppycrawl/tools/checkstyle/utils/commonutil/com/puppycrawl/tools/checkstyle/utils/commonutil, which would be a lot less comprehensive and a less good representation of what I'm trying to illustrate.

Copy link
Member

Choose a reason for hiding this comment

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

@romani What do you think of this?

Copy link
Member

Choose a reason for hiding this comment

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

I could move everything inside the "standard packaging", but at the expense of adding an excessively deep directory tree.

please deep path. Nobody will look at it, till deal with certain test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not so easy to fix after all. A good test must first fail without the fix (illustrating the issue) and succeed with the fix. The easy part is to make the test succeed with the fix. The hard part is to make it fail without the fix. That's why I have the two files by the way, the "good" file and the "bad" file. Whatever I try, there is always a combination where one of these files causes the AllTestsTest to fail. That is the reason it's taking me so long to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried, but I couldn't move the resources while still keeping AllTestsTest happy, and I certainly don't want to reduce the value of my test, so I left it the way it was because it's the only way I could preserve the value of the test while still satisfying AllTestsTest.

Copy link
Member

Choose a reason for hiding this comment

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

@peterdemaeyer what failure were you getting in AllTestsTest and what path were you setting it to?
The only reason AllTestsTest isn't flagging this now is because it is specifically looking in com/puppycrawl sub-folder which your file is not in.
You can add suppression to test and we can review later.

if (!path.contains(File.separatorChar + "grammar" + File.separatorChar)

private static boolean shouldSkipInputFileNameCheck(String path, String fileName) {

@peterdemaeyer peterdemaeyer force-pushed the issue-6263 branch 2 times, most recently from eacd920 to ff27f0d Compare April 28, 2019 08:24
@peterdemaeyer
Copy link
Contributor Author

The build fails because of what I believe to be an issue with the build system: it seems that openjdk 9/10/11/12 just fails to install on it.

Copy link
Member

@rnveach rnveach left a comment

Choose a reason for hiding this comment

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

Sorry, 1 final change.

@romani
Copy link
Member

romani commented May 17, 2019

@peterdemaeyer , ping.
It would be awesome to finish this PR

@peterdemaeyer
Copy link
Contributor Author

Yeah, sorry, I left this aside a bit after I ran into some trouble moving the resources around in an attempt to satisfy one of your remarks. I'll try picking it up again this weekend.

@romani
Copy link
Member

romani commented May 18, 2019

If problem is heavy .... please share it, we can reconsider decisions.

@peterdemaeyer peterdemaeyer force-pushed the issue-6263 branch 2 times, most recently from a73a6e8 to d78e355 Compare May 19, 2019 08:22
@peterdemaeyer
Copy link
Contributor Author

The remaining "problem" is the one about the test resources: the location of "good" and "bad" versions of InputCommonUtilTest_resources.txt. I thought I could move them to a subfolder, but then I hit some assertion of AllTestsTest. In the end, I didn't find a way to satisfy AllTestsTest without reducing the value of my test ("the value of my test" = not only does it have to succeed with the fix, it also has to fail for the right reason without the fix). Because I certainly don't want to reduce the value of my test, I just left it the way it was.

Additionally, I now run into a build issue I didn't have before, I still need to investigate that.

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

item to improve:

}

@Override
protected String getPackageLocation() {
Copy link
Member

Choose a reason for hiding this comment

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

please move this method to the top of class, right after fields.

Copy link
Member

Choose a reason for hiding this comment

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

It better to reply "done" when you finished review item.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK - I'll do that.
I expected the button "Resolve conversation" to do more or less the same, but apparently not (not sure what it actually does then).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

I expected the button "Resolve conversation" to do more or less the same, but apparently not

We don't get notifications on clicking resolve and pushing commits, so it could go unnotice that you made changes. Also we ask that we be the ones to click resolve conversation so we can use it as a way to double verify the work.

@romani
Copy link
Member

romani commented May 23, 2019

I hit some assertion of AllTestsTest

Please share error message.

@peterdemaeyer peterdemaeyer force-pushed the issue-6263 branch 2 times, most recently from 9088073 to 8bc51a0 Compare May 25, 2019 18:01
@peterdemaeyer
Copy link
Contributor Author

I finally managed to move the resource, and the AllTestsTest passes.

@peterdemaeyer
Copy link
Contributor Author

The build is stuck waiting for a build slave.

@rnveach
Copy link
Member

rnveach commented May 28, 2019

Jenkins can be ignored.

@rnveach
Copy link
Member

rnveach commented May 28, 2019

I finally managed to move the resource, and the AllTestsTest passes.

"com/puppycrawl/tools/checkstyle/utils/"
+ "com/puppycrawl/tools/checkstyle/utils/"

Why the double package in the folder path?

@rnveach rnveach assigned romani and unassigned rnveach May 28, 2019
@romani
Copy link
Member

romani commented Jun 1, 2019

@peterdemaeyer ,
Please answer question from @rnveach .

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

items to improve:

@Test
public void testGetUriByFilenameFindsResourceRelativeToRootClasspath() throws Exception {
final String filename =
"com/puppycrawl/tools/checkstyle/utils/commonutil/InputCommonUtilTest_resource.txt";
Copy link
Member

Choose a reason for hiding this comment

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

getPackageLocation() + "/InputCommonUtilTest_resource.txt"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider it done (commit pending).

final String uriRelativeToPackage =
"com/puppycrawl/tools/checkstyle/utils/"
+ "com/puppycrawl/tools/checkstyle/utils/"
+ "commonutil/InputCommonUtilTest_resource.txt";
Copy link
Member

Choose a reason for hiding this comment

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

getPackageLocation() + "/InputCommonUtilTest_resource.txt"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider it done (commit pending).

+ "com/puppycrawl/tools/checkstyle/utils/"
+ "commonutil/InputCommonUtilTest_resource.txt";
assertThat("URI is relative to package " + uriRelativeToPackage,
uri.toString(), not(containsString(uriRelativeToPackage)));
Copy link
Member

@romani romani Jun 1, 2019

Choose a reason for hiding this comment

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

please explain this assert , filename and uriRelativeToPackage are both relative . uriRelativeToPackage is bigger than filename so it will always have no assert. Do I miss something ?
We probably need some comments over code, as it is not obvious, or improve names of variables.

I think we need some summary from #6600 (comment)
with wording something like:

this test case is for "Issue #6232" without fix 
if user define 
  "com/puppycrawl/tools/checkstyle/utils/commonutil/InputCommonUtilTest_resource.txt"
it is was converted to 
"com/puppycrawl/tools/checkstyle/utils" 
+ "/"
+ "com/puppycrawl/tools/checkstyle/utils/commonutil/InputCommonUtilTest_resource.txt"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I'll explain everything in a Javadoc comment on the test method.
The assertion asserts that the URL does not contain the excessively deep path "com/puppycrawl/tools/checkstyle/utils/com/puppycrawl/tools/checkstyle/utils/commonutil".
It does fail without the fix.
With the fix, the location is not excessively deep, and the assertion succeeds.
Note that replacing the relevant portions of the strings with getPackageLocation() may make the test even less obvious, but I'll do it anyway.
Consider it done (commit pending).

IOUtils.copy(in, out);
}
assertThat("Content mismatches for: " + uri,
new String(out.toByteArray(), StandardCharsets.UTF_8), startsWith("good"));
Copy link
Member

Choose a reason for hiding this comment

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

String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8)
the simpler assertThat the better.

can we use https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/IOUtils.java#L2680 ? to skip all details on how actually content of file is loaded to String.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Consider it done (commit pending).

@peterdemaeyer
Copy link
Contributor Author

I finally managed to move the resource, and the AllTestsTest passes.

"com/puppycrawl/tools/checkstyle/utils/"
+ "com/puppycrawl/tools/checkstyle/utils/"

Why the double package in the folder path?

Because I want to illustrate that the "good" file is not in an excessively deep directory. It's only the "bad" file which is in the excessively deep directory.
Note that all this would have been easier to understand if I could have put the "good" file in the root (of the test resources) as I did initially. I was not too happy having to move them, precisely for this reason. Remember I said "at the cost of an excessively deep directory tree" - this is exactly what I meant.

I'll explain everything in a Javadoc comment on the test.

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

Ok to merge, thanks a lot for your efforts to finish implementation

@romani romani merged commit 9b8dea4 into checkstyle:master Jun 7, 2019
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.

3 participants