-
Notifications
You must be signed in to change notification settings - Fork 25.2k
This is for NoticeTask for issue #34459 #37032
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
Conversation
Pinging @elastic/es-core-infra |
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.
Thanks for your contribution @mike2ykme !
I left a few comments.
*/ | ||
public class NoticeTask extends DefaultTask { | ||
|
||
@InputFile |
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 annotations have no effect on the private fields, they need to be placed on public getters.
licensesDirs.add(licensesDir); | ||
} | ||
|
||
public void addLicenseDir(File licenseDir){ |
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 don't need another method that does the exact thing.
return Collections.unmodifiableList(this.licensesDirs); | ||
} | ||
|
||
// public static String getText(File file){ |
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.
please remove commented code.
|
||
public static void writeToFile(File file, String output, Charset charset){ | ||
|
||
try(BufferedWriter out = Files.newBufferedWriter(file.toPath(),charset)){ |
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.
would prefer to useFiles.write
for consistency here.
public class NoticeTaskTest extends GradleUnitTestCase { | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
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 Gradle test project creates it's own temporary folder no need to pass it in.
|
||
public List<File> getListWithoutCopies() throws IOException{ | ||
List<File> files = this.getListWithCopies(); | ||
files.remove(2); |
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 makes the test hard to read, I would rather create the list without copies here and add to it above.
|
||
|
||
try { | ||
noticeTask.generateNotice(); |
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 won't fail if an exception is not thrown as it should.
Use:
@Rule
public ExpectedException expectedEx = ExpectedException.none();
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.
I updated to now expect a RuntimeException with a certain a certain error message.
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 think you should use the ExpectedException
rule.
I don't think this test passes as it's written now.
The correct way would be to have a fail()
as the last line in try
, but the rule reads better.
} | ||
|
||
private NoticeTask createTask(Project project) { | ||
return (NoticeTask) project.task(singletonMap("type", NoticeTask.class), "NoticeTask"); |
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 cast can be avoided by using project.getTasks().create
removed commented out lines
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.
Thanks for the updates, I left some more comments.
final StringBuilder output = new StringBuilder(); | ||
|
||
|
||
try (Stream<String> lines = Files.lines(this.inputFile.toPath(), StandardCharsets.UTF_8)) { |
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.
please use a single line for spacing
licensesDirs.forEach(file -> { | ||
try (Stream<Path> pathStream = Files.walk(file.toPath())) { | ||
pathStream | ||
// .filter(path -> path.endsWith("-NOTICE.txt")) |
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.
no commented code please
seen.put(name, licenseFile); | ||
} | ||
}); | ||
} catch (IOException e) { |
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.
No need to catch this here.
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.
These are caught as checked and re-thrown as unchecked because they are being handled inside a lambda expression inside a forEach() on the list
builder.append(buffer, 0, read); | ||
} | ||
|
||
} catch (IOException e) { |
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's generally not desirable to catch IOException
low level, these should be bubbled up.
|
||
|
||
try { | ||
noticeTask.generateNotice(); |
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 think you should use the ExpectedException
rule.
I don't think this test passes as it's written now.
The correct way would be to have a fail()
as the last line in try
, but the rule reads better.
Removed TemporaryFolder and replaced with Gradle temporaryDir removed commented out code replaced getText with Apache FileUtils.readFileToString()
// party component names, unaffected by the full path to the various files | ||
final Map<String, File> seen = new TreeMap<>(); | ||
|
||
licensesDirs.forEach(file -> { |
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.
Is a forEach really necessary? A straight for loop would be just as readable, and would not require catching/rethrowing unchecked IOException
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.
Oh yeah that’s a good point. I’ll make the change.
pulled loop/stream to get notices out of given directories into separate function to allow throwing of IOException
@elasticmachine test this please |
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.*; |
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 don't allow * imports, this is why the build fails.
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 has been replaced with individual imports.
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.apache.commons.io.FileUtils.write; |
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.
commons io is only available via a transitive dependency in buildSrc
, it's not something we should be using, let's just stick to Files
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.
When I tried to assemble the project with just the Files functions I ran into issues where Charsets were not matching. IE UTF-8 wouldn't always be used. I've changed the function to simply ignore bad characters using the Java IO/NIO functions.
appendFileToOutput(licenseFile, name, "LICENSE", output); | ||
} | ||
|
||
Files.write(outputFile.toPath(), output.toString().getBytes()); |
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.
should specify a char set here.
Reading the files with Files.lines and such with just a Charset would fail if the file being read was not of the expected file format. I've now made it simply ignore the character to avoid any problems with the file being written back as UTF-8
} | ||
} | ||
return licensesSeen; | ||
} |
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 ran into issues with using the NIO Files methods using the UTF-8 charsets failing when they were reading the notice files and would run into a "java.nio.charset.MalformedInputException: Input length = 1" when running assemble command using Gradle. So I used the decoder here to cause it to replace the bad characters with the expected bad characters for UTF-8 so it can be clearly seen what characters were replaced.
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 prefer not to allow leniency in general, so it would be preferable to throw a meaningful error showing the file(s) that don't have the correct encoding and then fix those up rather than live with inconsistent encodings or broken characters.
@elasticmachine test this please
@mike2ykme are you still intersted in getting this in? |
Yes, is there anything I need to do for this? I thought it was just waiting on being tested. |
@mike2ykme I had a comment w.r.t. the bad encoding, and throwing an error and fixing them rather than ignoring it above. |
… fail cause any issues The NoticeTask will now throw a RuntimeException indicating which file caused the problem. The MalformedInputException is wrapped inside a java.io.UncheckedException by Files.lines(),
Thanks! I have now removed the code that would just ignore the bad character sets. Now it will throw an exception indicating there is a problem. I have noticed that Files.lines() wraps the MalformedInputException in a java.io.UncheckedException so I have caught these and rethrown a RuntimeException with the purpose of showing which file is causing the problem to avoid having to guess at where the problem is occuring at. |
thanks @mike2ykme ! @elasticmachine test this please |
Do we plan on merging this or shall we close it? |
I'm going to close it @mike2ykme feel free to reopen if you plan to work on this again |
Replace groovy build-tools (buildSrc) with java #34459