Uploaded image for project: 'SonarQube'
  1. SonarQube
  2. SONAR-9070

Make it easier to work with test files in unit tests

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.4
    • Component/s: Scanner
    • Labels:

      Description

      On Windows, when java.io.tmpdir contains shortened path, for example C:\Users\JANOSG~1\AppData\Local\Temp instead of the canonical C:\Users\Janos Gyerik\AppData\Local\Temp, the last assertion fails in this simplified scenario:

        @Rule
        public TemporaryFolder temp = new TemporaryFolder();
      
        @Test
        public void should_find_matching_file_regardless_of_OS() throws IOException {
          File moduleBaseDir = temp.getRoot();
          SensorContextTester context = SensorContextTester.create(moduleBaseDir);
      
          File file = temp.newFile();
          DefaultInputFile defaultFile = new TestInputFileBuilder("dummy project key", file.getName())
            .setModuleBaseDir(moduleBaseDir.toPath())
            .initMetadata("dummy content")
            .build();
      
          DefaultFileSystem fs = context.fileSystem();
          fs.add(defaultFile);
      
          // fails
          assertThat(fs.inputFile(fs.predicates().is(file))).isNotNull();
        }
      

      We can make it work by creating `file` like this:

          File file = file.toPath().toRealPath(new LinkOption[]{LinkOption.NOFOLLOW_LINKS}).toFile();
      

      This works, because this is the mechanism that DefaultFileSystem applies to its basedir parameter when created. In other words, DefaultFileSystem converts shortened paths to canonical path. And then, if file uses a shortened path, it will not be matched.

      A workaround is to specify a java.io.tmpdir using canonical path.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                duarte.meneses Duarte Meneses
                Reporter:
                janos.gyerik Janos Gyerik
              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: