github.com/solo-io/cue@v0.4.7/internal/cuetest/cuetest_test.go (about)

     1  package cuetest
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func TestCondition(t *testing.T) {
     9  	cases := []struct {
    10  		name string
    11  		env  string
    12  		con  string
    13  		want bool
    14  		err  string
    15  	}{
    16  		// issue cases covered by TestCheckIssueCondition
    17  		{
    18  			name: "long",
    19  			con:  "long",
    20  			want: Long, // not really testing much here
    21  		},
    22  		{
    23  			name: "bad condition",
    24  			env:  ".",
    25  			con:  "golang.org/Issue/1234", // note typo Issue
    26  			want: false,
    27  			err:  "unknown condition golang.org/Issue/1234",
    28  		},
    29  	}
    30  
    31  	for _, c := range cases {
    32  		t.Run(c.name, func(t *testing.T) {
    33  			os.Setenv(envNonIssues, c.env)
    34  			got, err := Condition(c.con)
    35  			if got != c.want {
    36  				t.Errorf("expected %v; got %v", c.want, got)
    37  			}
    38  			if c.err != "" {
    39  				if err == nil {
    40  					t.Errorf("expected error %q; got nil", c.err)
    41  				} else if e := err.Error(); e != c.err {
    42  					t.Errorf("expected error %q; got %q", c.err, e)
    43  				}
    44  			} else if err != nil {
    45  				t.Errorf("expected no error; got %v", err)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestCheckIssueCondition(t *testing.T) {
    52  	cases := []struct {
    53  		name     string
    54  		env      string
    55  		con      string
    56  		isIssue  bool
    57  		nonIssue bool
    58  		err      string
    59  	}{
    60  		{
    61  			name:     "empty env",
    62  			con:      "golang.org/issue/1234",
    63  			isIssue:  true,
    64  			nonIssue: false,
    65  		},
    66  		{
    67  			name:     "match all issues",
    68  			env:      ".",
    69  			con:      "golang.org/issue/1234",
    70  			isIssue:  true,
    71  			nonIssue: true,
    72  		},
    73  		{
    74  			name:    "bad issue URL",
    75  			con:     "golang.org/Issue/1234", // note typo
    76  			isIssue: false,
    77  		},
    78  		{
    79  			name:    "bad env",
    80  			env:     `\`,
    81  			con:     "golang.org/issue/1234",
    82  			isIssue: false,
    83  			err:     "failed to compile regexp \"\\\\\" specified via CUE_NON_ISSUES: error parsing regexp: trailing backslash at end of expression: ``",
    84  		},
    85  	}
    86  	for _, c := range cases {
    87  		t.Run(c.name, func(t *testing.T) {
    88  			os.Setenv(envNonIssues, c.env)
    89  			isIssue, nonIssue, err := checkIssueCondition(c.con)
    90  			if isIssue != c.isIssue {
    91  				t.Errorf("expected isIssue %v; got %v", c.isIssue, isIssue)
    92  			}
    93  			if nonIssue != c.nonIssue {
    94  				t.Errorf("expected nonIssue %v; got %v", c.nonIssue, nonIssue)
    95  			}
    96  			if c.err != "" {
    97  				if err == nil {
    98  					t.Errorf("expected error %q; got nil", c.err)
    99  				} else if e := err.Error(); e != c.err {
   100  					t.Errorf("expected error %q; got %q", c.err, e)
   101  				}
   102  			} else if err != nil {
   103  				t.Errorf("expected no error; got %q", err)
   104  			}
   105  		})
   106  	}
   107  
   108  }