Skip to content

Use trim() to remove trailing whitespace before using values from csv#45

Closed
skelliam wants to merge 1 commit into
jenkinsci:masterfrom
skelliam:fix_whitespace
Closed

Use trim() to remove trailing whitespace before using values from csv#45
skelliam wants to merge 1 commit into
jenkinsci:masterfrom
skelliam:fix_whitespace

Conversation

@skelliam

Copy link
Copy Markdown
Contributor

Use trim() on label values
Update test cases and test more than one csv file
Add test case that excludes only the last row

What has been done

  1. Use trim() on labels and y values
  2. Update test cases

Screenshots

Debugging in Visual Studio code, I took screenshots of the original code grabbing the trailing space from the new test case I added.

2018-03-25 17_25_01-csvseries java - plot-plugin - visual studio code
2018-03-25 17_27_10-csvseries java - plot-plugin - visual studio code

How to test

  1. If someone wants to reproduce this on Windows, I used this as a Windows batch script:
set plotfile=test.csv

IF NOT EXIST %plotfile% (
   echo foo,bar,thing > %plotfile%
)

echo %time:~-5%,7,%time:~-2% >> %plotfile%
  1. Now try to plot only the last column named "thing" using the plot plugin. It will not get plotted because the plugin only discovers the name "thing " (with space).

Checklist

  • Git commits follow best practices
  • Build passes in Jenkins
  • Appropriate tests or explanation to why this change has no tests
  • JIRA issue is well described (problem explanation, steps to reproduce, screenshots)
  • For dependency updates: links to external changelogs and, if possible, full diffs

Use trim() on label values
Update test cases and test more than one csv file
Add test case that excludes only the last row
// first create a FilePath to load the test Properties file.
File workspaceDirFile = new File("target/test-classes/");
FilePath workspaceRootDir = new FilePath(workspaceDirFile);
for (int testfilenum = 0; testfilenum < FILES.length; testfilenum++) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here I wrapped the test case with a for loop to test more than one csv file.

CSVSeries series = new CSVSeries(FILES[0], null, null, null, true);
assertTrue(series.getDisplayTableFlag());
CSVSeries series;
for (int testfilenum = 0; testfilenum < FILES.length; testfilenum++) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

again wrap existing test case with for loop to test multiple files

// first create a FilePath to load the test Properties file.
File workspaceDirFile = new File("target/test-classes/");
FilePath workspaceRootDir = new FilePath(workspaceDirFile);
for (int testfilenum = 0; testfilenum < FILES.length; testfilenum++) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

test multiple files again


// Create a new CSV series.
CSVSeries series = new CSVSeries(FILES[0], "http://localhost:8080/%name%/%index%/", "OFF", "", false);
public void testCSVSeriesIncludeOnlyLastColumn() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

new test case, include only the last column by name

@@ -0,0 +1,3 @@
foo,bar,thing
07.81,7,81
25.42,7,42 No newline at end of file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CSV file from Windows echo -- there is an extra space (0x20) character at the end of each line that messes up the plot plugin.

@skelliam

Copy link
Copy Markdown
Contributor Author

Photo of before/after:
image

@vgaidarji vgaidarji self-assigned this Mar 30, 2018
@vgaidarji
vgaidarji self-requested a review March 30, 2018 06:35

@vgaidarji vgaidarji left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It doesn't break plotting for me on Mac OS (don't have a Windows machine to check), and plots constructing for me even without this change.

Thanks for adding tests, those are really helpful.
Few tweaks are still required. Other than that everything is great.

++lineNum;
}
} catch (IOException e) {
assertTrue("Line count is not equal " + lineNum + " expected " + LINES[testfilenum], LINES[testfilenum] == lineNum);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For some reason, Checkstyle line length rule isn't enforced in tests. I need to fix that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But if you can reformat the code according to max line length = 100 rule, that would be awesome.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@vgaidarji For testing on MacOS, you only need to make sure that a trailing space is added at the end of the last label and the last value in each row. "echo" in MacOS or Linux probably does not have the same effect. The other thing you could do is remove the trim() statements I added in the functional code, but keep the tests the same. Then run the tests and see if they fail on MacOS.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@skelliam Yes, I did exactly that and it works for me even before trim() was added.
I have added spaces after the last label and after each last value in each line, and then selected that label in plot configuration and specified it without trailing space. Plots rendered just fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@vgaidarji, maybe it has something to do with the CR LF endings in Windows? If you don't implement trim(), do the new tests pass? If so that is really strange...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would really like to somehow have another user verify this -- I guess it is pretty low risk though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@skelliam yes, it's a very low risk. Trimming trailing spaces sounds very reasonable to me.
I've verified following cases:

  1. plot plugin build from 1ccbcc8
  • "column" & "value" & include column by name "column" = plots are shown
  • "column " & "value " & include column by name "column" = plots are shown
  • "column " & "value " & include column by name "column " = plots are not shown
  • "column" & "value" & include column by name "column " = plots are not shown
  1. Plot plugin 2.0.3 version
  • "column" & "value" & include column by name "column" = plots are shown
  • "column " & "value " & include column by name "column" = plots are shown
  • "column " & "value " & include column by name "column " = plots are not shown
  • "column" & "value" & include column by name "column " = plots are not shown

As you can see, the behavior is the same. I'm on Mac OS High Sierra 10.13.3.


I'm just wondering why I can't reproduce the issue you described and if it's OS related:

If someone wants to reproduce this on Windows, I used this as a Windows batch

set plotfile=test.csv

IF NOT EXIST %plotfile% (
   echo foo,bar,thing > %plotfile%
)

echo %time:~-5%,7,%time:~-2% >> %plotfile%

Now try to plot only the last column named "thing" using the plot plugin. It will not get plotted because the plugin only discovers the name "thing " (with space).

As I'm not getting plots when column name has the trailing space.

"column " & "value " & include column by name "column " = plots are not shown

@skelliam Could you please confirm if you have the same behavior?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also you can execute project tests on Windows machine by modifying the Jenkinsfile:

buildPlugin(platforms: ['linux', 'windows'])

or

buildPlugin() // 'linux', 'windows' by default

To be honest, I'm not sure if they pass or fail, never tried on this project and we used 'linux' when Jenkinsfile was added in #31.


private static final String[] FILES = {"test.csv"};
private static final String[] FILES = {"test.csv", "spacestest.csv", "test_trailing_semicolon.csv"};
private static final int[] LINES = {2, 3, 2}; //lines in the file including header

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comments should start with a space like // lines in ....
For some reason, this rule is also not enforced in tests.

CSVSeries series = new CSVSeries(FILES[0], null, null, null, true);
assertTrue(series.getDisplayTableFlag());
CSVSeries series;
for (int testfilenum = 0; testfilenum < FILES.length; testfilenum++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please follow camelCase in variables naming.
-> testFileNumber.

private static final int[] CORRECTED_COLS = {8, 3, 8}; //corrected for the trailing comma case
private static final int[] TOTAL_POINTS = {8, 6, 8}; //total data points in the file
private static final String[] LAST_COLUMN_NAME = {"error %", "thing", "error %"}; //the label on the last column
private static final String[] LAST_POINT = {"0.37", "42", "0.37"}; //the value of the last data point

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please remove unused constant.

File workspaceDirFile = new File("target/test-classes/");
FilePath workspaceRootDir = new FilePath(workspaceDirFile);
for (int testfilenum = 0; testfilenum < FILES.length; testfilenum++) {
// first create a FilePath to load the test Properties file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's extract following code block into a separate method and reuse in tests:

        // first create a FilePath to load the test Properties file.
        File workspaceDirFile = new File("target/test-classes/");
        FilePath workspaceRootDir = new FilePath(workspaceDirFile);

        LOGGER.info("workspace File path: " + workspaceDirFile.getAbsolutePath());
        LOGGER.info("workspace Dir path: " + workspaceRootDir.getName());

LOGGER.info("Got " + points.size() + " plot points");
testPlotPoints(points, TOTAL_POINTS[testfilenum]);

int pointnum = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please follow camelCase in variables naming.
-> pointNumber
-> columnNumber

@vgaidarji

Copy link
Copy Markdown
Contributor

@skelliam hey, are you still around?

@skelliam

Copy link
Copy Markdown
Contributor Author

@vgaidarji Hey I'm still around, just haven't had much of a chance to get to this... Seems like trim() is a low risk addition though, and you found other cases where you could use trim() i.e. on the column name to include?

@vgaidarji

Copy link
Copy Markdown
Contributor

@skelliam yes, let's fix the issues pointed above, try running tests on Windows slave (#45 (comment)), and we can merge this one.

@vgaidarji

Copy link
Copy Markdown
Contributor

@skelliam are you still around? ^^

@skelliam

skelliam commented Sep 1, 2018

Copy link
Copy Markdown
Contributor Author

Holiday weekend here in the US. Maybe I can get to this this weekend? Sorry for no feedback.

@codecov

codecov Bot commented Sep 20, 2018

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@658edf1). Click here to learn what that means.
The diff coverage is 41.86%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #45   +/-   ##
=========================================
  Coverage          ?   42.06%           
  Complexity        ?      178           
=========================================
  Files             ?       18           
  Lines             ?     1222           
  Branches          ?      186           
=========================================
  Hits              ?      514           
  Misses            ?      644           
  Partials          ?       64
Impacted Files Coverage Δ Complexity Δ
src/main/java/hudson/plugins/plot/PlotReport.java 47% <0%> (ø) 10 <0> (?)
src/main/java/hudson/plugins/plot/Plot.java 25.75% <0%> (ø) 30 <0> (?)
src/main/java/hudson/plugins/plot/PlotBuilder.java 62.16% <58.06%> (ø) 22 <7> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 658edf1...1ccbcc8. Read the comment docs.

@vgaidarji

Copy link
Copy Markdown
Contributor

@skelliam same question 🙂

are you still around? ^^

@vgaidarji

Copy link
Copy Markdown
Contributor

@skelliam I'll try to address remaining issues myself in #52. Didn't want to just abandon your work.
Thank you for your contribution 🎉

@skelliam

Copy link
Copy Markdown
Contributor Author

Thanks @vgaidarji, sorry to have abandoned this, I made the changes at work and since then haven't been close to our Jenkins build. 🙏

@vgaidarji

vgaidarji commented Jun 14, 2019

Copy link
Copy Markdown
Contributor

@skelliam no problem, it's understandable 😉 Thanks for your contribution 💪
New version 2.1.1 released and included into plugin wiki https://wiki.jenkins.io/display/JENKINS/Plot+Plugin.

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.

2 participants