github.com/joelanford/operator-sdk@v0.8.2/internal/pkg/scaffold/boilerplate_go_txt_test.go (about)

     1  // Copyright 2019 The Operator-SDK Authors
     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 scaffold
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/operator-framework/operator-sdk/internal/util/diffutil"
    21  
    22  	"github.com/spf13/afero"
    23  )
    24  
    25  func TestBoilerplate(t *testing.T) {
    26  	s, buf := setupScaffoldAndWriter()
    27  	s.Fs = afero.NewMemMapFs()
    28  	f, err := afero.TempFile(s.Fs, "", "")
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	if _, err = f.Write([]byte(boilerplate)); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	if err = f.Close(); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	s.BoilerplatePath = f.Name()
    39  	err = s.Execute(appConfig, &Apis{})
    40  	if err != nil {
    41  		t.Fatalf("Failed to execute the scaffold: (%v)", err)
    42  	}
    43  
    44  	if boilerplateExp != buf.String() {
    45  		diffs := diffutil.Diff(boilerplateExp, buf.String())
    46  		t.Fatalf("Expected vs actual differs.\n%v", diffs)
    47  	}
    48  }
    49  
    50  func TestBoilerplateMultiline(t *testing.T) {
    51  	s, buf := setupScaffoldAndWriter()
    52  	s.Fs = afero.NewMemMapFs()
    53  	f, err := afero.TempFile(s.Fs, "", "")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if _, err = f.Write([]byte(boilerplateMulti)); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if err = f.Close(); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	s.BoilerplatePath = f.Name()
    64  	err = s.Execute(appConfig, &Apis{})
    65  	if err != nil {
    66  		t.Fatalf("Failed to execute the scaffold: (%v)", err)
    67  	}
    68  
    69  	if boilerplateMultiExp != buf.String() {
    70  		diffs := diffutil.Diff(boilerplateMultiExp, buf.String())
    71  		t.Fatalf("Expected vs actual differs.\n%v", diffs)
    72  	}
    73  }
    74  
    75  const (
    76  	boilerplate = `// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac
    77  // velit a lacus tempor accumsan sit amet eu velit. Mauris orci lectus,
    78  // rutrum vitae porttitor in, interdum nec mauris. Praesent porttitor
    79  // lectus a sem volutpat, ac fringilla magna fermentum. Donec a nibh
    80  // a urna fringilla eleifend. Curabitur vitae lorem nulla. Ut at risus
    81  // varius, blandit risus quis, porta tellus. Vivamus scelerisque turpis
    82  // quis viverra rhoncus. Aenean non arcu velit.
    83  `
    84  	boilerplateExp   = boilerplate + "\n" + apisExp
    85  	boilerplateMulti = `/*
    86  Copyright The Project Authors.
    87  
    88  Licensed under the Apache License, Version 2.0 (the "License");
    89  you may not use this file except in compliance with the License.
    90  You may obtain a copy of the License at
    91  
    92      http://www.apache.org/licenses/LICENSE-2.0
    93  
    94  Unless required by applicable law or agreed to in writing, software
    95  distributed under the License is distributed on an "AS IS" BASIS,
    96  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    97  See the License for the specific language governing permissions and
    98  limitations under the License.
    99  */
   100  `
   101  	boilerplateMultiExp = boilerplateMulti + "\n" + apisExp
   102  )