Stefan S.
Stefan S.

Reputation: 4103

File Content Suddenly Changes On Jenkins

This problem is a real headache. I have some code to extract a ZIP file which looks like this:

    public File read(InputStream input) throws IOException {
        try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
                final ZipInputStream inputStream = new ZipInputStream(bufferedInputStream);) {

            final File outputFolder = Files.createTempDirectory(null).toFile();

            ZipEntry entry;
            while ((entry = inputStream.getNextEntry()) != null) {
                final File outputFile = new File(outputFolder, entry.getName());
                extractEntry(inputStream, outputFile);
            }
            return outputFolder;
        }
    }

    private void extractEntry(InputStream is, File outputFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(outputFile);) {

            final byte[] buf = new byte[this.bufferSize];

            int length;
            while ((length = is.read(buf, 0, buf.length)) >= 0) {
                fos.write(buf, 0, length);
            }
        }
    }

My tests for this class just compares the file content I put into the ZIP with the file above code extracts:

Assert.assertArrayEquals(Files.readAllBytes(referenceFile), Files.readAllBytes(extractedFile)); 

Which should work in any case, right? It does for me. The length of the files is 973 bytes on my machine.

However when run on Jenkins the very same tests with fail with the following exception:

java.lang.AssertionError: array lengths differed, expected.length=996 actual.length=973

The actual numbers are fix, so the expected length is suddenly higher. I habe no idea how that could possibly happen. When I download the files, they certainly look identical, even in a byte by byte comparison.

What is the problem?

I made sure both machines run Open JDK 11.0.2, so for once the Java version shouldn't be the problem.

Upvotes: 1

Views: 122

Answers (1)

Stefan S.
Stefan S.

Reputation: 4103

So I found the solution while putting the question together, but I'll leave the answer here for myself and anyone else with mysterious problems:

It's Git. It's always Git with this kind of problems. Why would it be something different than Git?

Sometimes and without warning, Git decides that your line breaks are not good enough, and changes them to something else. My guess is since the byte count went up it added "\r" before or after my line break of "\n". Guess Git lost interest when it came to the ZIP files, and only changed the reference files.

So when my code extracted the ZIPs, they had the original line breaks, but the reference files had the Git changed ones, so they weren't identical.

And while you could probably make a case for "Git randomly adding line breaks is a good thing", I have no idea why Git doesn't add them to your local repository as well. I'd found the source of the problem a lot sooner if just my copy and the Jenkins copy were identical.

Note: I found the problem because I thought it was a weird coincidence that the 23 bytes appeared on the Jenkins, when my file had 23 lines. So if you have a similar problem, try checking for something like this.

Upvotes: 1

Related Questions