Skip to content

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

Closed
wants to merge 10 commits into from

Conversation

mike2ykme
Copy link

Replace groovy build-tools (buildSrc) with java #34459

@dnhatn dnhatn added the :Delivery/Build Build or test infrastructure label Dec 30, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

Copy link
Contributor

@alpar-t alpar-t left a 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
Copy link
Contributor

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){
Copy link
Contributor

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){
Copy link
Contributor

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)){
Copy link
Contributor

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();
Copy link
Contributor

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);
Copy link
Contributor

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();
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

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");
Copy link
Contributor

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

Copy link
Contributor

@alpar-t alpar-t left a 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)) {
Copy link
Contributor

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"))
Copy link
Contributor

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) {
Copy link
Contributor

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.

Copy link
Author

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) {
Copy link
Contributor

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();
Copy link
Contributor

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 -> {
Copy link
Member

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

Copy link
Author

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
@alpar-t
Copy link
Contributor

alpar-t commented Jan 10, 2019

@elasticmachine test this please

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
Copy link
Contributor

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.

Copy link
Author

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;
Copy link
Contributor

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

Copy link
Author

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());
Copy link
Contributor

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;
}
Copy link
Author

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.

Copy link
Contributor

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

@danielmitterdorfer
Copy link
Member

@mike2ykme are you still intersted in getting this in?

@mike2ykme
Copy link
Author

Yes, is there anything I need to do for this? I thought it was just waiting on being tested.

@alpar-t
Copy link
Contributor

alpar-t commented Feb 26, 2019

@mike2ykme I had a comment w.r.t. the bad encoding, and throwing an error and fixing them rather than ignoring it above.
Note that you'll also have to merge master in to get the checks passing.

… 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(),
@mike2ykme
Copy link
Author

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.

@alpar-t
Copy link
Contributor

alpar-t commented Feb 28, 2019

thanks @mike2ykme !
I suspect we'll now have a failing build that will highlight the bad engodings.
We won't be able to merge this RP if it fails the build, so we'll also have to fix those files in here.

@elasticmachine test this please

@javanna
Copy link
Member

javanna commented Jul 15, 2019

Do we plan on merging this or shall we close it?

@alpar-t
Copy link
Contributor

alpar-t commented Jul 16, 2019

I'm going to close it @mike2ykme feel free to reopen if you plan to work on this again

@alpar-t alpar-t closed this Jul 16, 2019
@mark-vieira mark-vieira added the Team:Delivery Meta label for Delivery team label Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Delivery/Build Build or test infrastructure Team:Delivery Meta label for Delivery team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants