Saturday, January 28, 2023
HomeiOS DevelopmentCode protection for Swift Package deal Supervisor primarily based apps

Code protection for Swift Package deal Supervisor primarily based apps


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

Bitrise

The best 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’ll be able to construct and run these apps and you’ve got the flexibility to write down unit assessments in your codebase. Xcode ships with the XCTest framework, however you might not know that that is an open supply library. It is accessible on each single platform the place you’ll be able to set up Swift. This additionally implies 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 bundle. 📦

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

mkdir "myProject" && cd $_


swift bundle init


swift bundle init --type=executable


Each the library and the executable template accommodates a pattern take a look at file with a dummy take a look at case. You’ll be able to run assessments in some ways, there’s built-in assist for parallel execution (you’ll be able to even specify the variety of employees), you may also filter what to run by take a look at goal, take a look at case or you’ll be able to 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:


Check Suite 'All assessments' 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 take a look at, with 0 failures (0 sudden) in 0.070 (0.070) seconds
Check 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
Check Suite 'All assessments' 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 it is advisable to course of the end result of the testing, that may be fairly difficult. I’ve created a small instrument that may convert your take a look at outcomes right into a JSON file. It is known as Testify, you’ll be able to 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’ll be able to’t use the –parallel flag on this case, as a result of for those who accomplish that you may solely get progress indication as a substitute of the ultimate take a look at outcome output. Happily, you’ll be able to nonetheless filter assessments, so you do not have to attend for every part.


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


If every part went properly you may see a pleasant JSON output, identical to this one:


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

          ],
          "startDate" : 602416925.19000006,
          "circumstances" : [
            {
              "outcome" : "success",
              "className" : "myProjectTests",
              "moduleName" : "myProjectTests",
              "testName" : "testExample",
              "duration" : 0.062
            }
          ],
          "sudden" : 0,
          "consequence" : "success",
          "title" : "myProjectTests"
        }
      ],
      "startDate" : 602416925.19000006,
      "circumstances" : [

      ],
      "sudden" : 0,
      "consequence" : "success",
      "title" : "myProjectPackageTests.xctest"
    }
  ],
  "startDate" : 602416925.19000006,
  "circumstances" : [

  ],
  "sudden" : 0,
  "consequence" : "success",
  "title" : "Chosen assessments"
}



Enabling code protection knowledge

Code protection is a measurement of what number of strains/blocks/arcs of your code are executed whereas the automated assessments are working.

I consider that protection stories are extraordinarily helpful for the whole developer staff. Challenge managers can seek advice from the protection proportion if it involves software program high quality. The QA staff may look at protection stories & take a look at all of the remaining elements or counsel new take a look at concepts for the builders. Programmers can get rid of many of the bugs by writing correct unit / UI assessments for the applying. A protection report helps them to analyse what must be carried out as properly. Xcode has a built-in protection report web page, however it’s a must to allow stories first. You’ll be able to 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 high quality, however the place is my report? 🤔



The best way to show protection knowledge?

To this point so good, you might have generated the code protection report recordsdata, however they’re nonetheless in a very complicated file format. You want yet another further instrument as a way to show them correctly.


sudo apt-get set up llvm


brew set up llvm

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

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

Now you might be prepared to make use of llvm-cov which is a part of the LLVM infrastructure. You’ll be able to learn extra about it by working man llvm-cov, however I am going to present you how you can show some primary 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|Checks" 
    -use-color

This command will generate the protection report in your assessments, 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 range primarily based 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 positioned underneath the identical listing that is not a giant distinction, however nonetheless watch out with the platform title. Often you do not wish to embody the recordsdata out of your .construct & Checks listing, however you’ll be able to specify your individual regex primarily based filter as properly. 🔍



Placing every part 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 work out every part primarily based on the present undertaking. Save your self a couple 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


It is best to reserve it as cov.sh or one thing related. Add some permissions through the use of chmod +x cov.sh and you might be able to run it by merely getting into ./cov.sh. Your protection report will appear to be 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%

In fact for those who run this script on a undertaking that has extra supply recordsdata & unit assessments, it will produce a greater report. 😜



Conclusion

Utilizing take a look at outcomes and protection knowledge is a pleasant option to present stories to different members in your staff. By working these instructions on a steady integration server (like Bitrise), you’ll be able to automate your complete workflow.




Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments