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

fix: Test failure in TestModule.testModuleNames() (#4285) #4286

Merged
merged 3 commits into from
Nov 15, 2021
Merged

fix: Test failure in TestModule.testModuleNames() (#4285) #4286

merged 3 commits into from
Nov 15, 2021

Conversation

xzel23
Copy link
Contributor

@xzel23 xzel23 commented Nov 13, 2021

This fixes #4285. The test relied on the order of entries returned by File#listFiles(), which is unspecified (thank you @SirYwell ).

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Hi @xzel23, thanks for fixing the flaky test! I propose we go one step further to improve it while we're at it, see comment :)

Comment on lines 317 to 319
List<String> list = Arrays.stream(cu).map(CompilationUnit::getModuleName).map(String::copyValueOf).collect(Collectors.toList());
assertTrue(list.contains("foo"));
assertTrue(list.contains("bar"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

While you're poking about here anyway, how about improving it just a little bit more? This test is a bit weak (because it leaves the possibility of there being other module names in the list), and it also isn't entirely clear about its purpose.

I propose we assert on the entire list. And instead of a list use a set, because we care only about distinct module names.

Suggested change
List<String> list = Arrays.stream(cu).map(CompilationUnit::getModuleName).map(String::copyValueOf).collect(Collectors.toList());
assertTrue(list.contains("foo"));
assertTrue(list.contains("bar"));
Set<String> moduleNames = Arrays.stream(cu)
.map(CompilationUnit::getModuleName)
.map(String::valueOf)
.collect(Collectors.toSet());
assertThat(moduleNames, equalTo(Set.of("bar", "foo")));

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. I replaced the suggested deprecated assertThat with a non-deprecated one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I didn't suggest the deprecated assertThat, I just didn't specify which one to use (in fact I didn't even remember that JUnit 4 had a built-in assertThat). You've imported both, so I suspect your IDE auto-imported a bunch of stuff. See review comments.

Comment on lines 20 to 21
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not needed

Suggested change
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;

@@ -54,11 +56,16 @@
import java.util.Collections;
import java.util.ArrayList;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think ArrayList is needed anymore.

Suggested change
import java.util.ArrayList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
import static org.junit.Assert.assertThat;

assertTrue(list.contains("foo"));
assertTrue(list.contains("bar"));
Set<String> list = Arrays.stream(cu).map(CompilationUnit::getModuleName).map(String::copyValueOf).collect(Collectors.toSet());
MatcherAssert.assertThat(list, is(Set.of("foo", "bar")));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
MatcherAssert.assertThat(list, is(Set.of("foo", "bar")));
assertThat(list, is(Set.of("foo", "bar")));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My IDE had somehow lost track of the imports and marked everything as missing, so I didn't see the unused exports. Fixed after restart. I have cleaned it up and added a static import for the hamcrest assertThat as suggested by the junit deprecationcomment. Hope it's alright now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I feel you. I find that most IDEs with default settings are over-eager when it comes to auto-importing stuff, it gets rather confusing rather quickly.

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Thanks @xzel23, this is a much more robust (and non-flaky) test!

@slarse slarse merged commit 103ea93 into INRIA:master Nov 15, 2021
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.

[Bug] Test failure in TestModule.testModuleNames()
2 participants