HomeiOS DevelopmentCode protection for Swift Bundle Supervisor based mostly apps

Code protection for Swift Bundle Supervisor based mostly apps


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

Bitrise

The best way to check utilizing SPM?

The Swift Bundle Supervisor means that you can create standalone Swift purposes each on Linux and macOS. You possibly can construct and run these apps and you’ve got the flexibility to put in writing unit exams to 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 accessible on each single platform the place you possibly can set up Swift. This additionally signifies that you need to use the very same assertion strategies from the framework that you just used to work with on iOS to unit check your SPM bundle. 📦

Let me present you how you can make a model new undertaking utilizing the Swift Bundle Supervisor:

mkdir "myProject" && cd $_


swift bundle init


swift bundle init --type=executable


Each the library and the executable template accommodates a pattern check file with a dummy check case. You possibly can run exams in some ways, there may be built-in help for parallel execution (you possibly can even specify the variety of staff), you may as well filter what to run by check goal, check case or you possibly can consider only one check. ✅



swift check


swift check -l   #or `swift check --list-tests`


swift check --parallel


swift check --parallel --num-workers 2



swift check --filter myProjectTests.myProjectTests


The check result’s going to look considerably like this:


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



Processing check outcomes

If it’s essential to course of the result of the testing, that may be fairly difficult. I’ve created a small device that may convert your check outcomes right into a JSON file. It is known as Testify, you possibly can seize it from GitHub. Let me present you the way it works:

swift check 2>&1 | testify
swift check --filter myProjectTests.myProjectTests 2>&1 | testify


Sadly, you possibly can’t use the –parallel flag on this case, as a result of when you achieve this you may solely get progress indication as an alternative of the ultimate check consequence output. Fortuitously, you possibly can nonetheless filter exams, so you do not have to attend for the whole lot.


The swift check command returns the check outcomes on the usual error, as an alternative of the usual output. That is why you need to redirect the stderr into the stdout by way of the 2>&1 flag.


If the whole lot went nicely you may see a pleasant JSON output, identical 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
            }
          ],
          "surprising" : 0,
          "final result" : "success",
          "identify" : "myProjectTests"
        }
      ],
      "startDate" : 602416925.19000006,
      "instances" : [

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

  ],
  "surprising" : 0,
  "final 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 stories are extraordinarily helpful for the complete developer workforce. Undertaking managers can seek advice from the protection share if it involves software program high quality. The QA workforce can even study protection stories & check all of the remaining components or recommend new check 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 achieved as nicely. Xcode has a built-in protection report web page, however you need to allow stories first. You possibly can obtain the very same factor with out utilizing Xcode, by merely offering an additional flag to the check command:

swift check --enable-code-coverage

Okay, that is high-quality, however the place is my report? 🤔



The best way to show protection information?

Thus far so good, you’ve got generated the code protection report information, however they’re nonetheless in a extremely advanced file format. You want yet another further device as a way 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 might be 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 will present you how you can show some fundamental protection report for the pattern undertaking.


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|Exams" 
    -use-color

This command will generate the protection report to your exams, however provided that you’ve offered the --enable-code-coverage flag throughout testing. It is best to be aware that these llvm-cov enter paths might fluctuate based mostly in your present system. If you’re 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 positioned below the identical listing that is not an enormous distinction, however nonetheless watch out with the platform identify. Often you do not wish to embrace the information out of your .construct & Exams listing, however you possibly can specify your individual regex based mostly filter as nicely. 🔍



Placing the whole lot collectively

You do not wish to fiddle with these parameters, proper? Neither do I. That is why I made a helpful shell script that may work out the whole lot based mostly on the present undertaking. Save your self just a few 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|Exams" 
    -use-color


It is best to put it aside as cov.sh or one thing related. Add some permissions by utilizing chmod +x cov.sh and you might be able to run it by merely getting into ./cov.sh. Your protection report will seem like this:



Filename            Areas    Missed Areas     Cowl   Capabilities  Missed Capabilities  Executed       Traces      Missed Traces     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%

After all when you run this script on a undertaking that has extra supply information & unit exams, it’s going to produce a greater report. 😜



Conclusion

Utilizing check outcomes and protection information is a pleasant option to present stories to different members in your workforce. By operating these instructions on a steady integration server (like Bitrise), you possibly can automate your total workflow.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments