Uploaded image for project: 'Swift'
  1. Swift
  2. SR-7446

Code coverage generation using the Swift 4.1 toolchain doesn't reach conditional code paths in init methods

    XMLWordPrintable

    Details

      Description

      Since upgrading to Xcode 9.3/Swift 4.1, I noticed that code coverage percentage dropped in several of my projects.  

      The bug seems to occur when I use conditional code within an initializer: 

      init(name: String, age: Int, role: Role) {
          self.name = name
          self.age = age
      
          if self.age >= 18 {
              self.ofAge = true
          } else {
              self.ofAge = false
          }
      
          switch role {
          case .designer:
              self.tool = "Photoshop"
          case .engineer:
              self.tool = "Xcode"
          case .tester:
              self.tool = "Mail"
          }
      }
      

      Neither the if/else branches nor the switch cases are counted towards line coverage when compiling with Xcode 9.3 and "Gather coverage" turned on. 

      I wrote test cases (see attached project "Codepath.zip") that exercise the code paths in question and verified their execution with the debugger.
      Older Xcode versions or the swift-4.0.3-RELEASE-osx toolchain from swift.org report correct percentages when I enable code coverage in the Test phase of the scheme.

      I also tested the behaviour with a command line tool (see attached project "codepath-cli.zip") and

      xcrun swiftc -profile-generate -profile-coverage-mapping main.swift
      ./main
      xcrun llvm-profdata merge -o testprof.profdata default.profraw
      xcrun llvm-cov show ./main -instr-profile=testprof.profdata main.swift

      When using Xcode 9.3's default toolchain, conditional code within the initializer is not reached. When using the 4.0.3 final toolchain, all conditional code is counted correctly (at least once). 

      The test CLI tool project contains xcrun-based scripts to generate & show coverage for the Xcode default toolchain (showCoverageSwift41.sh) and the final 4.0.3 toolchain (showCoverageSwift403). 

        Attachments

        1. Codepath.zip
          34 kB
        2. codepath-cli.zip
          26 kB

          Activity

            People

            Assignee:
            vedantk Vedant Kumar
            Reporter:
            weichsel Thomas Zoechling
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: