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

Objc : Unable to locate test source file ****.swift #242

Closed
egr-ext opened this issue Aug 22, 2019 · 31 comments
Closed

Objc : Unable to locate test source file ****.swift #242

egr-ext opened this issue Aug 22, 2019 · 31 comments
Labels

Comments

@egr-ext
Copy link
Contributor

egr-ext commented Aug 22, 2019

Describe the bug
The sonar report has 0 tests reported but the coverage is ok.

I saw these warnings:

INFO: Processing Surefire report TEST-report.xml
INFO: Unable to locate test source file Tests_GenericViewController.swift
INFO: Unable to locate test source file Tests_GenericViewController.m
INFO: Unable to locate test source file Tests_GenericViewController.swift

But as my project is in Objective C the test file names end with .m not .swift.
So the tool does not find the files.

@davidy4ng
Copy link
Contributor

From those logs, it looks like your test is contained in a test class named GenericViewController (wether it's swift or objective-c).

-Did you set the sonar.tests path property in sonar-project.properties file?
-Make sure your test class file is in that path (or in a folder in that path)
-Make sure your test class name matches its file name.

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 22, 2019

Hi David, here are my answers:

-Did you set the sonar.tests path property in sonar-project.properties file?
Yes:
sonar.tests=MyApp-unitTests

it is the directory where the tests files are.

-Make sure your test class file is in that path (or in a folder in that path)
Yes they are
-Make sure your test class name matches its file name.
Yes : class name: Tests_GenericViewController and the class file Tests_GenericViewController.m

My understanding is that the tool look for Tests_GenericViewController.swift but it should be Tests_GenericViewController.m ???

@gaelfoppolo
Copy link
Collaborator

Did you set sonar.language?

@davidy4ng
Copy link
Contributor

@egr-ext actually it looks for both. That's why you also have the log
INFO: Unable to locate test source file Tests_GenericViewController.m

Can you provide a sample of your TEST-report.xml file?

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 23, 2019

Did you set sonar.language?

yes: sonar.language=objc

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 23, 2019

@egr-ext actually it looks for both. That's why you also have the log
INFO: Unable to locate test source file Tests_GenericViewController.m

Can you provide a sample of your TEST-report.xml file?

here is an extract:

<?xml version='1.0' encoding='UTF-8'?>
<testsuites name='Notico-unitTests.xctest' tests='504' failures='0'>
  <testsuite name='Tests_AngelContactGenericTableViewCell' tests='6' failures='0'>
    <testcase classname='Tests_AngelContactGenericTableViewCell' name='testDrawImage' time='0.006'/>
    <testcase classname='Tests_AngelContactGenericTableViewCell' name='testSelectAndShowBellIcon' time='0.005'/>
    <testcase classname='Tests_AngelContactGenericTableViewCell' name='testUpdateViewWithCompany' time='0.001'/>
    <testcase classname='Tests_AngelContactGenericTableViewCell' name='testUpdateViewWithContact' time='0.003'/>
    <testcase classname='Tests_AngelContactGenericTableViewCell' name='testUpdateViewWithInvitationReceived' time='0.001'/>
    <testcase classname='Tests_AngelContactGenericTableViewCell' name='testWriteAlertLabel' time='0.002'/>
  </testsuite>
  <testsuite name='Tests_AngelContactsAddressBookViewController' tests='2' failures='0'>
    <testcase classname='Tests_AngelContactsAddressBookViewController' name='testGetCreatedPerson' time='0.009'/>
    <testcase classname='Tests_AngelContactsAddressBookViewController' name='testSelectCreatedPerson' time='0.010'/>
  </testsuite>

@egr-ext egr-ext changed the title Objc projetc : Unable to locate test source file ****.swift Objc : Unable to locate test source file ****.swift Aug 23, 2019
@gaelfoppolo
Copy link
Collaborator

The classname should be Tests.AngelContactGenericTableViewCelland not Tests_AngelContactGenericTableViewCell, for example.
Does your filename contains a + or -?
PR #239 should fix this issue then.

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 26, 2019

The filename is Tests_AngelContactGenericTableViewCell.m
The class name is Tests_AngelContactGenericTableViewCell

Here is an objc sample:

file path: ./MyApp-unitTests/Tests_AngelContactGenericTableViewCell.m

@interface Tests_AngelContactGenericTableViewCell : XCTestCase
@property (strong, nonatomic) AngelContactGenericTableViewCell *cell;
@property (strong, nonatomic) PLContact *contact;
@end

@implementation Tests_AngelContactGenericTableViewCell

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 26, 2019

The classname should be Tests.AngelContactGenericTableViewCelland not Tests_AngelContactGenericTableViewCell, for example.
Does your filename contains a + or -?
PR #239 should fix this issue then.

I tried with backelite-sonar-swift-plugin-0.4.5.jar but I still does NOT have the number pf tests reported.

Is it an issue with tests files name containing "_" ?

@gaelfoppolo
Copy link
Collaborator

Indeed I think your issue is related to that.
The plugin is looking to a dot sepator and since yours is an underscore, it does not found it.
Maybe we should add the capability to support both.
Could you try removing the understore to see if the issue dissapears?

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 26, 2019

Hi Gael,

Yes but no success again:
1/ I just changed the file name and class name to "TestsAngelContactGenericTableViewCell" and TestsAngelContactGenericTableViewCell.m

//
//  TestsAngelContactGenericTableViewCell.m
//  Notico-unitTests
//
//  Created by **** on 10/07/2019.
//  Copyright © 2019 ****. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "AngelContactGenericTableViewCell.h"
#import "Invitation+CoreDataClass.h"

@interface TestsAngelContactGenericTableViewCell : XCTestCase
@property (strong, nonatomic) AngelContactGenericTableViewCell *cell;
@property (strong, nonatomic) PLContact *contact;
@end

@implementation TestsAngelContactGenericTableViewCell

2/ I ran the analysis

-> in Sonar I can see all indicators (coverage) but the number of tests is still missing.

I have no clue ....

Additionnal info: I ran an analysis using the same environnement (CI, Tools, ...) on a basic "Hello Word" swift project and the number of tests is showing in Sonar.

For info: I have this warning:

[14:06:06]: ▸ 14:06:06.515 INFO: Unable to locate Swift test source file for classname TestsAngelContactGenericTableViewCell. Make sure your test class name matches its filename.
[14:06:06]: ▸ 14:06:06.515 INFO: Unable to locate Objective-C test source file for classname TestsAngelContactGenericTableViewCell. Make sure your test class name matches its filename.

@davidy4ng
Copy link
Contributor

Which version of xcode and xcpretty do you use?

Looking back at your TEST-report.xml file, I see that the value of classname is : Tests_AngelContactGenericTableViewCell

In mine, I have the package name as a prefix, something like MyTests.MyClassnameTests.

Here is how it currently works: given a test report, a test case with classname MyTests.MyClassnameTests, the plugin will replace . by /, to get a hypothetical path for the test class.

So it searches for MyTests/MyClassnameTests.swift or MyTests/MyClassnameTests.m.

If no file is found, it then takes the last path component, here MyClassnameTests.swift or MyClassnameTests.m and search for any file with that name in the tests source folder you provided in your sonar-project.properties with the key sonar.tests

If nothing is found, it can't provide the tests to your SonarQube report.

Does it help?

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 26, 2019

Which version of xcode and xcpretty do you use?

Xcode: Version 10.3 (10G8)
xcpretty: 0.3.0

Looking back at your TEST-report.xml file, I see that the value of classname is : Tests_AngelContactGenericTableViewCell

In mine, I have the package name as a prefix, something like MyTests.MyClassnameTests.

Yes but I can see only that on a swift project. On an Objective C project it seems it is not prefixed by the directory name

Here is how it currently works: given a test report, a test case with classname MyTests.MyClassnameTests, the plugin will replace . by /, to get a hypothetical path for the test class.

So it searches for MyTests/MyClassnameTests.swift or MyTests/MyClassnameTests.m.

If no file is found, it then takes the last path component, here MyClassnameTests.swift or MyClassnameTests.m and search for any file with that name in the tests source folder you provided in your sonar-project.properties with the key sonar.tests

If nothing is found, it can't provide the tests to your SonarQube report.

My project file structure is like that :
./MyApp/Sources/**** -> all source files to be analysed
./MyApp-unitTests/Tests_******.m -> all test files

In the file sonar-project.properties I configured the tests directory:

sonar.tests=MyApp-unitTests

Here is the Fastfile for Fastlane:

  lane :unitTest do
    scan(
      scheme: "MyApp-unitTests",
      clean: true,
      device: "iPhone X",
      code_coverage: true,
      derived_data_path: "./DerivedData",
      output_directory: "./sonar-reports",
      output_types: "junit,json-compilation-database",
      output_files: "TEST-report.xml,compile_commands.json",
      use_clang_report_name: true
    )

    slather(
      cobertura_xml: true,
      jenkins: true,
      scheme: "MyApp-unitTests",
      build_directory: "./DerivedData",
      output_directory: "./sonar-reports",
      source_directory: "MyApp/Sources",
      proj: "MyAppFNo.xcodeproj",
      workspace: "MyApp.xcworkspace",
      binary_basename: "MyApp-preprod"
    )

    sh("mv #{ENV['PWD']}/sonar-reports/cobertura.xml #{ENV['PWD']}/sonar-reports/coverage-cobertura.xml")

    oclint(
      compile_commands: "sonar-reports/compile_commands.json",
      report_type: 'pmd',
      select_regex: /Sources/,
      exclude_regex: /Pods/,
      max_priority_1: 9999,
      max_priority_2: 9999,
      max_priority_3: 9999,
      thresholds: ["LONG_LINE=200", "LONG_METHOD=200"],
      enable_clang_static_analyzer: false,
      allow_duplicated_violations: true,
      report_path: "./sonar-reports/oclint.xml", 
      extra_arg: "-Wno-everything"
    )

    sonar
  end

Does it help?

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 26, 2019

Anyway can we re-open this issue ?

@davidy4ng
Copy link
Contributor

Your scan command seems to be running directly on test scheme.
Try running it on your app scheme instead. If your project is setup correctly, it should run the related test scheme.

That may be the reason why your junit report file doesn't include the package name as a prefix.

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 26, 2019

I get the same result. They are not prefixed with package name.

  • One new info:
    I just created a clean project with Xcode called MyApp (the standard 1 ViewController app , in language Objc).
    I ran the analysis on the app scheme on this brand new project.
    The number of tests still does not appear on SonarQube.
    All other metrics appear.

  • Another info
    If I perform the equivalent in Swift i.e create a standard Swift project and I perform the analysis.
    The SonarQube report is showing the Number of Tests indicator in the dashboard.

My guess is the issue is related to Objective C language analysis.

@gaelfoppolo
Copy link
Collaborator

Can you provide this project sample so we can reproduce? (attach the zip here)

@gaelfoppolo gaelfoppolo reopened this Aug 26, 2019
@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 27, 2019

Here it is:
MyApp.zip

@gaelfoppolo
Copy link
Collaborator

Looks like the Junit is malformated.
Please use the .sh script to compare and fix your Fastlane integration.

@davidy4ng
Copy link
Contributor

You might also want to have a look at my PR #241

For fastlane integration with the same behavior as the bash script

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 27, 2019

Looks like the Junit is malformated.
Please use the .sh script to compare and fix your Fastlane integration.

Sorry I don't understand which script ?

@gaelfoppolo
Copy link
Collaborator

The script detailed in the README, section Launching the analysis.

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 27, 2019

OK.

The script keep saying some swift parameters are missing ex: sonar.swift.appScheme parameter is missing in sonar-project.properties. You must specify which scheme is used to build your application.

But I have a Objective C project ... DO I have to set swift parameters ?

Plus I use Fastlane so I use this script to understand but it is not easy to find why it does not work with Fastlane

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 27, 2019

So I ran the script run-sonar-swift.sh.

I have the same result.

TEST-report.xml:

<?xml version='1.0' encoding='UTF-8'?>
<testsuites name='MyAppUITests.xctest' tests='3' failures='0'>
  <testsuite name='MyAppTests' tests='2' failures='0'>
    <testcase classname='MyAppTests' name='testExample' time='0.001'/>
    <testcase classname='MyAppTests' name='testPerformanceExample' time='0.338'/>
  </testsuite>
  <testsuite name='MyAppUITests' tests='1' failures='0'>
    <testcase classname='MyAppUITests' name='testExample' time='4.708'/>
  </testsuite>
</testsuites>

And I don't have the numer of tests.
sonar

Here is the zip of the objc project:
MyApp.zip

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 28, 2019

I'am 100% convinced that it is a bug for Objective C source code only.
Can someone direct me to the code that process these files ?

@gaelfoppolo or @davidy4ng do you have an ObjC project that works as an example I can check ?

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 28, 2019

I added some logs to the Logger..
The ligne 39 don't find the test file but the filename is ok and in the path:
FilePredicate fp = fileSystem.predicates().hasPath(fileName);

I keep analysing

@egr-ext
Copy link
Contributor Author

egr-ext commented Aug 28, 2019

I found the issue !!!
Fixed by this PR #248

@davidy4ng
Copy link
Contributor

Thanks @egr-ext

@gaelfoppolo
Copy link
Collaborator

Nice work @egr-ext! I'll have a look at your PR!

zippy1978 added a commit that referenced this issue Jan 24, 2020
Fix issue #242 when objc test classes and files are not found
@zippy1978
Copy link
Contributor

PR merged !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants