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

Integration tests fail: TempFolder not working if TMP/TEMP are not set correct #1949

Closed
elviiix opened this issue Oct 5, 2020 · 12 comments
Closed

Comments

@elviiix
Copy link

elviiix commented Oct 5, 2020

Description

Build of sonar-cxx on Windows with OpenJDK produces errors

Steps to reproduce the problem

Install OpenJDK from https://adoptopenjdk.net/index.html
(either OpenJDK 8 or OpenJDK 11).

Install Apache Maven 3.6.3

run
mvn clean install

Expected behavior

build finishes without errors

Actual behavior

Some tests result in errors
[ERROR] Failures:
[ERROR] CxxReportPatternMatchingTest.getReports_patternMatching:87 [Failed for pattern: {}] expected:<[[C:\Users\ALEXMI~1\AppData\Local\Temp\junit9123077941808277150\A.ext]]> but was:<[[]]>
[ERROR] CxxReportSensor_getReports_Test.testAbsoluteInsideBasedir:48 expected:<[1]> but was:<[0]>
[ERROR] CxxReportSensor_getReports_Test.testAbsoluteOutsideBasedirAndRelative:84 expected:<[2]> but was:<[1]>
[ERROR] CxxReportSensor_getReports_Test.testAbsoluteOutsideBasedirWithGlobbingAndNestedRelativeWithGlobbing:121 expected:<[6]> but was:<[2]>
[ERROR] CxxReportSensor_getReports_Test.testAbsoluteOutsideBasedirWithGlobbingAndRelativeWithGlobbing:103 expected:<[7]> but was:<[2]>
[ERROR] CxxReportSensor_getReports_Test.testRelativeBackticksOutsideBasedirThenBackInside:134 expected:<[4]> but was:<[0]>

Known workarounds

Commenting out tests mentioned above results in successful build

Related information

build results in theese errors both for 1.3.2 and master (I was able to build master only with JDK 11).

P.S. And one more thing. Can you allow underscore character in clang-tidy rules, it's allowed there by clang-tidy itself and warnings not importing in SonarQube was rather unexpected.

--- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/clangtidy/CxxClangTidySensor.java
+++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/clangtidy/CxxClangTidySensor.java
@@ -101,7 +101,7 @@ public class CxxClangTidySensor extends CxxIssuesReportSensor {
                 ruleId = txt.substring(i + 1, txt.length() - 1);
                 break;
               }
-              if (!(Character.isLetterOrDigit(c) || c == '-' || c == '.')) {
+              if (!(Character.isLetterOrDigit(c) || c == '-' || c == '.' || c == '_')) {
                 break;
               }
             }
-- 

@guwirth
Copy link
Collaborator

guwirth commented Oct 6, 2020

Hello @elviiix,

we are using https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/.travis.yml and https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/appveyor.yml to build and verify the plugin, both are working. Maybe you can have a look into the log files if you see any differences.

  • cxx 1.3 for SQ 7.9 LTS is working with OpenJDK 11
  • for SQ 6.7 LTS you have to compile it with Java 8
  • cxx 2.0 is working only with OpenJDK 11

underscore character in clang-tidy rules

The allowed characters are not defined by us, they are defined from SQ core. We can't change this.

Regards,

@elviiix
Copy link
Author

elviiix commented Oct 6, 2020

we are using https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/.travis.yml and https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/appveyor.yml to build and verify the plugin, both are working. Maybe you can have a look into the log files if you see any differences.

Ok, I'll try to have a look later.

underscore character in clang-tidy rules

The allowed characters are not defined by us, they are defined from SQ core. We can't change this.

About underscore - with patch I provided in first message - SQ 7.9 successfully imports rules. They are filtered by your code and not SonarQube.

@guwirth
Copy link
Collaborator

guwirth commented Oct 6, 2020

About underscore - with patch I provided in first message - SQ 7.9 successfully imports rules. They are filtered by your code and not SonarQube.

@elviiix thx, I will verify this.

@elviiix
Copy link
Author

elviiix commented Oct 8, 2020

Tried to debug a little.
For
org.sonar.cxx.sensors.utils.CxxReportPatternMatchingTest.txt
i'm getting a warning
WARN Property 'sonar.cxx.cppcheck.reportPath': cannot find any files matching the Ant pattern(s) 'C:\TMP\junit5396464083749204555\A.ext'

First for some reason TemporaryFolder from
CxxReportPatternMatchingTest.java

  @Rule
  public TemporaryFolder base = new TemporaryFolder();

is not created. If I remove @rule string it creates ok, but even with that in
CxxReportSensor.java
String[] existingReportPaths = directoryScanner.getIncludedFiles();
returns empty array in spite of existance of file in normalizedReportPaths.

I'll try to have a more detailed look when a have a free time, but can you possibly comment about these problems?

@guwirth
Copy link
Collaborator

guwirth commented Oct 8, 2020

@Rule
public TemporaryFolder base = new TemporaryFolder();

https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgarygregory.wordpress.com%2F2010%2F01%2F20%2Fjunit-tip-use-rules-to-manage-temporary-files-and-folders%2F&amp;data=02%7C01%7Cguenter.wirth%40etas.com%7C5184285fbe9f4d62b33908d86b76eff6%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637377505854972211&amp;sdata=RZAJ%2Bv3OZO1r4BGIowlmp7cPpL%2FB2trR%2FiUEKtQGo7A%3D&amp;reserved=0

@elviiix something special with your temp folder or OS?
Missing access right?

The TemporaryFolder creates a folder in the default temporary file directory specified by the system property java.io.tmpdir.

java.io.tmpdir pointing to an invalid folder?
Step through the test and see if you find files under 'java.io.tmpdir'?

@elviiix
Copy link
Author

elviiix commented Oct 8, 2020

@elviiix something special with your temp folder or OS?
Missing access right?

No nothing special, tried even setting to C:/Tmp using environment variables, same effect.
And as I have said if I remove @rule and make TemporaryFolder base a regular member variable it's being created successfully, so it's probably not a problem with rights.

I have tried on 2 computers - one with Win10, second with Windows Server and have the same errors during build on both.

java.io.tmpdir pointing to an invalid folder?
Step through the test and see if you find files under 'java.io.tmpdir'?

As I have already said then I make base a member variable - I do see files in right location (don't know junit well enough to understand why it works this way and not another), but DirectoryScanner is still unable to find them.

P.S. To me it looks more like problems with directory separator and not access rights.

@elviiix
Copy link
Author

elviiix commented Oct 9, 2020

Found out the reason.

Problem is in case sensitivity. Windows by default sets temp directory inside user profile. And if user name is in uppercase path in environmnent variable has that part of path in uppercase but the profile folder itself on disk is created in lowercase. And it looks like TemporaryFolder has a problem with such setting. I think you can reproduce the problem if you set env variable on Windows to differ in case from real folder.

With this patch 1.3.2 builds successfully. Not sure it's the best way, but i think idea is clear (If you plan on changing anything probaly it would be better to alter other TemporaryFolder as well).

--- a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxReportPatternMatchingTest.java
+++ b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxReportPatternMatchingTest.java
@@ -37,10 +37,23 @@ public class CxxReportPatternMatchingTest {
 
   private static final String REPORT_PATH_KEY = "sonar.cxx.cppcheck.reportPath";
   @Rule
-  public TemporaryFolder base = new TemporaryFolder();
+  public TemporaryFolder base = getTempFolder();
   private final MapSettings settings = new MapSettings();
   private final List<String[]> examples = new LinkedList<>();
 
+
+  static TemporaryFolder getTempFolder()
+  {
+	 try
+	 {
+	    return new TemporaryFolder(new File(System.getProperty("java.io.tmpdir")).getCanonicalFile());
+	 }
+	 catch(java.io.IOException e)
+	 {
+		return new TemporaryFolder();
+	 }
+  }
+
   @Before
   public void setUp() {
     //                        "pattern",      "matches",   "matches not"
--- a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxReportSensor_getReports_Test.java
+++ b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/utils/CxxReportSensor_getReports_Test.java
@@ -34,9 +34,21 @@ public class CxxReportSensor_getReports_Test {
   private static final String REPORT_PATH_KEY = "sonar.cxx.cppcheck.reportPath";
 
   @Rule
-  public TemporaryFolder base = new TemporaryFolder();
+  public TemporaryFolder base = getTempFolder();
   private final MapSettings settings = new MapSettings();
 
+  static TemporaryFolder getTempFolder()
+  {
+	 try
+	 {
+	    return new TemporaryFolder(new File(System.getProperty("java.io.tmpdir")).getCanonicalFile());
+	 }
+	 catch(java.io.IOException e)
+	 {
+		return new TemporaryFolder();
+	 }
+  }
+
   @Test
   public void testAbsoluteInsideBasedir() throws IOException {
     File absReportFile = new File(base.getRoot(), "path/to/report.xml").getAbsoluteFile();
-- 

@guwirth
Copy link
Collaborator

guwirth commented Oct 12, 2020

@elviiix thx for your investigations, I will have a look to it.

@Bhushetti
Copy link

Bhushetti commented Sep 30, 2021

hi,

I am getting this error The plugin [cxx] does not support Java 1.8.0_275 in my jenkins when i build my java project.

Is it enough to upgrade the Jenkins, SonarQube & SonarScanner to Java 11 to use the cxx 2.0 plugin or I must also upgrade my source code to Java 11? I cannot currently upgrade my source code to Java 11.

Is there any other alternate solution to use the 3 Metrics (Public API, Public Documented API (%) & Public Undocumented API) in sonarqube? or directly in the Java sourceCode perhaps?

@guwirth
Copy link
Collaborator

guwirth commented Sep 30, 2021

Hi @Bhushetti,

for the cxx plugin you have to use Java 11 , see https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Upgrade-the-Plugin

Is it enough to upgrade the Jenkins, SonarQube & SonarScanner to Java 11 to use the cxx 2.0 plugin or I must also upgrade my source code to Java 11?

Think you are speaking about your Java source code? You can use Java 11 VM but analyse Java 8. For that it's recommended to use the setting sonar.java.source: see description on page https://docs.sonarqube.org/latest/analysis/languages/java/.

Is there any other alternate solution to use the 3 Metrics (Public API, Public Documented API (%) & Public Undocumented API)

The cxx plugin works only with C/C++ code. So I don't see how you can use it with Java code?

Regards,

@guwirth
Copy link
Collaborator

guwirth commented Feb 11, 2022

The component TemporaryFolder needs a valid TMP / TEMP folder setting:

  • environment TMP / TEMP variables must exist
  • they must point to valid folders
  • path separators must fit to OS
  • upper / lower case of path must fit

Sample for GitHub Actions:

# for tests on Windows we need a valid TMP folder
# - necessary for org.junit.rules.TemporaryFolder
#
- name: Adapt TMP folder on Windows OS
   shell: bash
   run: |
     echo "TMP=$USERPROFILE\AppData\Local\Temp" >> $GITHUB_ENV
     echo "TEMP=$USERPROFILE\AppData\Local\Temp" >> $GITHUB_ENV

@guwirth guwirth changed the title Build of sonar-cxx on Windows with OpenJDK produces errors Integration tests fail: TempFolder not working if TMP/TEMP are not set correct Feb 11, 2022
@guwirth guwirth self-assigned this Feb 11, 2022
@guwirth guwirth closed this as completed Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants