github.com/thediveo/gons@v0.9.9/reexec/testing/profile_test.go (about)

     1  // Copyright 2020 Harald Albrecht.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testing
    16  
    17  import (
    18  	"os"
    19  
    20  	. "github.com/onsi/ginkgo/v2"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("coverage profile data", func() {
    25  
    26  	It("rejects invalid coverage profile data files", func() {
    27  		if os.Getegid() == 0 {
    28  			Skip("only non-root")
    29  		}
    30  
    31  		cp := newCoverageProfile()
    32  		Expect(func() { mergeCoverageFile("test/nonexisting.cov", cp) }).NotTo(Panic())
    33  		Expect(cp.Sources).To(BeEmpty())
    34  
    35  		Expect(func() { mergeCoverageFile("/root", cp) }).To(Panic())
    36  		Expect(cp.Sources).To(BeEmpty())
    37  
    38  		Expect(func() { mergeCoverageFile("test/empty.cov", cp) }).NotTo(Panic())
    39  		Expect(cp.Sources).To(BeEmpty())
    40  
    41  		Expect(func() { mergeCoverageFile("test/modeless.cov", cp) }).To(Panic())
    42  		Expect(cp.Sources).To(BeEmpty())
    43  
    44  		Expect(func() { mergeCoverageFile("test/broken1.cov", cp) }).To(Panic())
    45  		Expect(cp.Sources).To(BeEmpty())
    46  
    47  		Expect(func() { mergeCoverageFile("test/broken2.cov", cp) }).To(Panic())
    48  		Expect(cp.Sources).To(BeEmpty())
    49  
    50  		Expect(func() { mergeCoverageFile("test/broken3.cov", cp) }).To(Panic())
    51  		Expect(cp.Sources).To(BeEmpty())
    52  	})
    53  
    54  	It("reads coverage profile data", func() {
    55  		cp := newCoverageProfile()
    56  		Expect(func() { mergeCoverageFile("test/cov1.cov", cp) }).NotTo(Panic())
    57  		Expect(cp.Mode).To(Equal("atomic"))
    58  		Expect(cp.Sources).To(HaveLen(2))
    59  		Expect(cp.Sources).To(HaveKey("a/b.go"))
    60  		Expect(cp.Sources).To(HaveKey("a/c.go"))
    61  		Expect(cp.Sources["a/b.go"].Blocks).To(HaveLen(2))
    62  		Expect(cp.Sources["a/b.go"].Blocks[0]).To(Equal(coverageProfileBlock{
    63  			StartLine: 1,
    64  			StartCol:  0,
    65  			EndLine:   2,
    66  			EndCol:    42,
    67  			NumStmts:  3,
    68  			Counts:    456,
    69  		}))
    70  	})
    71  
    72  	It("rejects merging different modes", func() {
    73  		cp := newCoverageProfile()
    74  		Expect(func() { mergeCoverageFile("test/cov1.cov", cp) }).NotTo(Panic())
    75  		Expect(func() { mergeCoverageFile("test/set.cov", cp) }).To(Panic())
    76  	})
    77  
    78  	It("merges mode \"atomic\"", func() {
    79  		cp := newCoverageProfile()
    80  		Expect(func() { mergeCoverageFile("test/cov1.cov", cp) }).NotTo(Panic())
    81  		Expect(func() { mergeCoverageFile("test/cov2.cov", cp) }).NotTo(Panic())
    82  		Expect(cp.Sources).To(HaveLen(3))
    83  		Expect(cp.Sources["a/b.go"].Blocks).To(HaveLen(2))
    84  		Expect(cp.Sources["a/c.go"].Blocks).To(HaveLen(1))
    85  		Expect(cp.Sources["a/d.go"].Blocks).To(HaveLen(2))
    86  		Expect(cp.Sources["a/b.go"].Blocks[0]).To(Equal(coverageProfileBlock{
    87  			StartLine: 1,
    88  			StartCol:  0,
    89  			EndLine:   2,
    90  			EndCol:    42,
    91  			NumStmts:  3,
    92  			Counts:    457,
    93  		}))
    94  	})
    95  
    96  	It("merges mode \"set\"", func() {
    97  		cp := newCoverageProfile()
    98  		Expect(func() { mergeCoverageFile("test/set.cov", cp) }).NotTo(Panic())
    99  		Expect(func() { mergeCoverageFile("test/set.cov", cp) }).NotTo(Panic())
   100  		Expect(cp.Sources).To(HaveLen(2))
   101  		Expect(cp.Sources["a/b.go"].Blocks[0]).To(Equal(coverageProfileBlock{
   102  			StartLine: 1,
   103  			StartCol:  0,
   104  			EndLine:   2,
   105  			EndCol:    42,
   106  			NumStmts:  3,
   107  			Counts:    1,
   108  		}))
   109  	})
   110  
   111  })