github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/release/formula/main_test.go (about)

     1  // Copyright 2022 The kpt 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 main
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"net/http"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestFormula(t *testing.T) {
    27  	want := `# Copyright 2019 The kpt Authors
    28  #
    29  # Licensed under the Apache License, Version 2.0 (the "License");
    30  # you may not use this file except in compliance with the License.
    31  # You may obtain a copy of the License at
    32  #
    33  #      http://www.apache.org/licenses/LICENSE-2.0
    34  #
    35  # Unless required by applicable law or agreed to in writing, software
    36  # distributed under the License is distributed on an "AS IS" BASIS,
    37  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    38  # See the License for the specific language governing permissions and
    39  # limitations under the License.
    40  
    41  class Kpt < Formula
    42    desc "Toolkit to manage,and apply Kubernetes Resource config data files"
    43    homepage "https://googlecontainertools.github.io/kpt"
    44    url "https://github.com/GoogleContainerTools/kpt/archive/v0.0.0-fake.tar.gz"
    45    sha256 "4e42c5ce1a23511405beb5f51cfe07885fa953db448265fe74ee9b81e0def277"
    46  
    47    depends_on "go" => :build
    48  
    49    def install
    50      ENV["GO111MODULE"] = "on"
    51      system "go", "build", "-ldflags", "-X github.com/GoogleContainerTools/kpt/run.version=#{version}", *std_go_args
    52    end
    53  
    54    test do
    55      assert_match version.to_s, shell_output("#{bin}/kpt version")
    56    end
    57  end
    58  `
    59  
    60  	httpClient := &http.Client{
    61  		Transport: &fakeServer{t: t},
    62  	}
    63  	url := "https://github.com/GoogleContainerTools/kpt/archive/v0.0.0-fake.tar.gz"
    64  	got, err := buildFormula(httpClient, url)
    65  	if err != nil {
    66  		t.Fatalf("error from buildFormula(%q): %v", url, err)
    67  	}
    68  	if diff := cmp.Diff(want, got); diff != "" {
    69  		t.Errorf("buildFormula(%q) returned unexpected diff (-want +got):\n%s", url, diff)
    70  	}
    71  }
    72  
    73  type fakeServer struct {
    74  	t *testing.T
    75  }
    76  
    77  func (s *fakeServer) RoundTrip(req *http.Request) (*http.Response, error) {
    78  	if req.URL.Path != "/GoogleContainerTools/kpt/archive/v0.0.0-fake.tar.gz" {
    79  		s.t.Errorf("Expected to request '/GoogleContainerTools/kpt/archive/v0.0.0-fake.tar.gz', got: %s", req.URL.Path)
    80  	}
    81  	body := bytes.NewReader([]byte(`This is fake content so that our tests don't download big files`))
    82  	return &http.Response{
    83  		Status:     http.StatusText(http.StatusOK),
    84  		StatusCode: http.StatusOK,
    85  		Body:       io.NopCloser(body),
    86  	}, nil
    87  }