github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/hack/gen-prow-documented/main_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"os"
    21  	"path"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  func TestGen(t *testing.T) {
    28  	type FakeReporter struct {
    29  		// Context is the name of the GitHub status context for the job.
    30  		// Defaults: the same as the name of the job.
    31  		Context string `json:"context,omitempty"`
    32  		// SkipReport skips commenting and setting status on GitHub.
    33  		SkipReport bool `json:"skip_report,omitempty"`
    34  	}
    35  
    36  	tests := []struct {
    37  		name            string
    38  		rawContents     []byte
    39  		expectedRawYaml []byte
    40  	}{
    41  		{
    42  			name: "also-read-raw",
    43  			rawContents: []byte(`package main
    44  type FakeReporter struct {
    45  	// Context is the name of the GitHub status context for the job.
    46  	// Defaults: the same as the name of the job.
    47  	Context string ` + "`" + `json:"context,omitempty"` + "`" + `
    48  	// SkipReport skips commenting and setting status on GitHub.
    49  	SkipReport bool ` + "`" + `json:"skip_report,omitempty"` + "`" + `
    50  }`),
    51  			expectedRawYaml: []byte(`# Context is the name of the GitHub status context for the job.
    52  # Defaults: the same as the name of the job.
    53  context: ' '
    54  # SkipReport skips commenting and setting status on GitHub.
    55  skip_report: true
    56  `),
    57  		},
    58  	}
    59  
    60  	for _, tc := range tests {
    61  		tc := tc
    62  		t.Run(tc.name, func(t *testing.T) {
    63  			tmpDir := t.TempDir()
    64  			in, out := tc.name+".in", tc.name+".out"
    65  			if err := os.WriteFile(path.Join(tmpDir, in), tc.rawContents, 0644); err != nil {
    66  				t.Fatalf("Failed creating input: %v", err)
    67  			}
    68  			g := genConfig{
    69  				in: []string{
    70  					in,
    71  				},
    72  				format: &FakeReporter{},
    73  				out:    out,
    74  			}
    75  
    76  			if err := g.gen(tmpDir); err != nil {
    77  				t.Fatalf("Got unexpected error: %v", err)
    78  			}
    79  
    80  			got, err := os.ReadFile(path.Join(tmpDir, out))
    81  			if err != nil {
    82  				t.Fatalf("Failed reading out: %v", err)
    83  			}
    84  			if diff := cmp.Diff(string(tc.expectedRawYaml), string(got)); diff != "" {
    85  				t.Fatalf("Mismatch:\n%s", diff)
    86  			}
    87  		})
    88  	}
    89  }