Monday, September 26, 2022
HomeiOS DevelopmentCode protection for Swift Package deal Supervisor based mostly apps

Code protection for Swift Package deal Supervisor based mostly apps


Discover ways to collect and show code protection studies in your Swift packages each for macOS and Linux with out utilizing Xcode in any respect.

Bitrise

The way to take a look at utilizing SPM?

The Swift Package deal Supervisor lets you create standalone Swift purposes each on Linux and macOS. You possibly can construct and run these apps and you’ve got the flexibility to write down unit exams in your codebase. Xcode ships with the XCTest framework, however it’s possible you’ll not know that that is an open supply library. It is out there on each single platform the place you may set up Swift. This additionally signifies that you should utilize the very same assertion strategies from the framework that you simply used to work with on iOS to unit take a look at your SPM package deal. 📦

Let me present you learn how to make a model new venture utilizing the Swift Package deal Supervisor:

mkdir "myProject" && cd $_


swift package deal init


swift package deal init --type=executable


Each the library and the executable template comprises a pattern take a look at file with a dummy take a look at case. You possibly can run exams in some ways, there’s built-in assist for parallel execution (you may even specify the variety of employees), you can too filter what to run by take a look at goal, take a look at case or you may consider only one take a look at. ✅



swift take a look at


swift take a look at -l   #or `swift take a look at --list-tests`


swift take a look at --parallel


swift take a look at --parallel --num-workers 2



swift take a look at --filter myProjectTests.myProjectTests


The take a look at result’s going to look considerably like this:


Take a look at Suite 'All exams' began at 2020-01-16 16:58:23.584
Take a look at Suite 'myProjectPackageTests.xctest' began at 2020-01-16 16:58:23.584
Take a look at Suite 'myProjectTests' began at 2020-01-16 16:58:23.584
Take a look at Case '-[myProjectTests.myProjectTests testExample]' began.
Take a look at Case '-[myProjectTests.myProjectTests testExample]' handed (0.070 seconds).
Take a look at Suite 'myProjectTests' handed at 2020-01-16 16:58:23.654.
     Executed 1 take a look at, with 0 failures (0 sudden) in 0.070 (0.070) seconds
Take a look at Suite 'myProjectPackageTests.xctest' handed at 2020-01-16 16:58:23.655.
     Executed 1 take a look at, with 0 failures (0 sudden) in 0.070 (0.071) seconds
Take a look at Suite 'All exams' handed at 2020-01-16 16:58:23.655.
     Executed 1 take a look at, with 0 failures (0 sudden) in 0.070 (0.071) seconds



Processing take a look at outcomes

If you must course of the end result of the testing, that may be fairly difficult. I’ve created a small device that may convert your take a look at outcomes right into a JSON file. It is referred to as Testify, you may seize it from GitHub. Let me present you the way it works:

swift take a look at 2>&1 | testify
swift take a look at --filter myProjectTests.myProjectTests 2>&1 | testify


Sadly, you may’t use the –parallel flag on this case, as a result of in case you accomplish that you will solely get progress indication as an alternative of the ultimate take a look at outcome output. Thankfully, you may nonetheless filter exams, so you do not have to attend for all the things.


The swift take a look at command returns the take a look at outcomes on the usual error, as an alternative of the usual output. That is why it’s important to redirect the stderr into the stdout through the 2>&1 flag.


If all the things went nicely you will see a pleasant JSON output, similar to this one:


{
  "endDate" : 602416925.25200009,
  "kids" : [
    {
      "endDate" : 602416925.25200009,
      "children" : [
        {
          "endDate" : 602416925.25200009,
          "children" : [

          ],
          "startDate" : 602416925.19000006,
          "instances" : [
            {
              "outcome" : "success",
              "className" : "myProjectTests",
              "moduleName" : "myProjectTests",
              "testName" : "testExample",
              "duration" : 0.062
            }
          ],
          "sudden" : 0,
          "end result" : "success",
          "identify" : "myProjectTests"
        }
      ],
      "startDate" : 602416925.19000006,
      "instances" : [

      ],
      "sudden" : 0,
      "end result" : "success",
      "identify" : "myProjectPackageTests.xctest"
    }
  ],
  "startDate" : 602416925.19000006,
  "instances" : [

  ],
  "sudden" : 0,
  "end result" : "success",
  "identify" : "Chosen exams"
}



Enabling code protection information

Code protection is a measurement of what number of traces/blocks/arcs of your code are executed whereas the automated exams are operating.

I consider that protection studies are extraordinarily helpful for the complete developer group. Venture managers can consult with the protection share if it involves software program high quality. The QA group also can look at protection studies & take a look at all of the remaining elements or recommend new take a look at concepts for the builders. Programmers can get rid of many of the bugs by writing correct unit / UI exams for the applying. A protection report helps them to analyse what must be carried out as nicely. Xcode has a built-in protection report web page, however it’s important to allow studies first. You possibly can obtain the very same factor with out utilizing Xcode, by merely offering an additional flag to the take a look at command:

swift take a look at --enable-code-coverage

Okay, that is advantageous, however the place is my report? 🤔



The way to show protection information?

Thus far so good, you will have generated the code protection report recordsdata, however they’re nonetheless in a extremely complicated file format. You want yet another further device in an effort to show them correctly.


sudo apt-get set up llvm


brew set up llvm

echo 'export PATH="/usr/native/decide/llvm/bin:$PATH"' >> ~/.zshrc

echo 'export PATH="/usr/native/decide/llvm/bin:$PATH"' >> ~/.bashrc

Now you’re prepared to make use of llvm-cov which is a part of the LLVM infrastructure. You possibly can learn extra about it by operating man llvm-cov, however I am going to present you learn how to show some primary protection report for the pattern venture.


llvm-cov report 
    .construct/x86_64-apple-macosx/debug/myProjectPackageTests.xctest/Contents/MacOS/myProjectPackageTests 
    -instr-profile=.construct/x86_64-apple-macosx/debug/codecov/default.profdata 
    -ignore-filename-regex=".construct|Checks" 
    -use-color

This command will generate the protection report in your exams, however provided that you’ve offered the --enable-code-coverage flag throughout testing. You must be aware that these llvm-cov enter paths might differ based mostly in your present system. In case you are utilizing Linux, you must merely give the xctest path as a parameter (e.g. .construct/x86_64-unknown-linux/debug/myProjectPackageTests.xctest on this case), the instrument profile is situated below the identical listing that is not an enormous distinction, however nonetheless watch out with the platform identify. Often you do not wish to embody the recordsdata out of your .construct & Checks listing, however you may specify your personal regex based mostly filter as nicely. 🔍



Placing all the things collectively

You do not wish to fiddle with these parameters, proper? Neither do I. That is why I made a useful shell script that may determine all the things based mostly on the present venture. Save your self a number of hours, right here is the ultimate snippet:




BIN_PATH="$(swift construct --show-bin-path)"
XCTEST_PATH="$(discover ${BIN_PATH} -name '*.xctest')"

COV_BIN=$XCTEST_PATH
if [[ "$OSTYPE" == "darwin"* ]]; then
    f="$(basename $XCTEST_PATH .xctest)"
    COV_BIN="${COV_BIN}/Contents/MacOS/$f"
fi

llvm-cov report 
    "${COV_BIN}" 
    -instr-profile=.construct/debug/codecov/default.profdata 
    -ignore-filename-regex=".construct|Checks" 
    -use-color


You must reserve it as cov.sh or one thing comparable. Add some permissions by utilizing chmod +x cov.sh and you’re able to run it by merely coming into ./cov.sh. Your protection report will seem like this:



Filename            Areas    Missed Areas     Cowl   Capabilities  Missed Capabilities  Executed       Strains      Missed Strains     Cowl-------------------------------------------------------------------------------------------------------------------------------------
myProject.swift           3                 0   100.00%           3                 0   100.00%           8                 0   100.00%-------------------------------------------------------------------------------------------------------------------------------------
TOTAL                     3                 0   100.00%           3                 0   100.00%           8                 0   100.00%

In fact in case you run this script on a venture that has extra supply recordsdata & unit exams, it will produce a greater report. 😜



Conclusion

Utilizing take a look at outcomes and protection information is a pleasant option to present studies to different members in your group. By operating these instructions on a steady integration server (like Bitrise), you may automate your complete workflow.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular