|
| 1 | +// Copyright 2025 Google LLC |
| 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 languagecontainer |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "cloud.google.com/java/internal/librariangen/languagecontainer/release" |
| 25 | + "cloud.google.com/java/internal/librariangen/message" |
| 26 | + "github.com/google/go-cmp/cmp" |
| 27 | +) |
| 28 | + |
| 29 | +func TestRun(t *testing.T) { |
| 30 | + tmpDir := t.TempDir() |
| 31 | + if err := os.WriteFile(filepath.Join(tmpDir, "release-init-request.json"), []byte("{}"), 0644); err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + args []string |
| 37 | + wantCode int |
| 38 | + wantErr bool |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "unknown command", |
| 42 | + args: []string{"foo"}, |
| 43 | + wantCode: 1, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "build command", |
| 47 | + args: []string{"build"}, |
| 48 | + wantCode: 1, // Not implemented yet |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "configure command", |
| 52 | + args: []string{"configure"}, |
| 53 | + wantCode: 1, // Not implemented yet |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "generate command", |
| 57 | + args: []string{"generate"}, |
| 58 | + wantCode: 1, // Not implemented yet |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "release-init command success", |
| 62 | + args: []string{"release-init", "-librarian", tmpDir}, |
| 63 | + wantCode: 0, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "release-init command failure", |
| 67 | + args: []string{"release-init", "-librarian", tmpDir}, |
| 68 | + wantCode: 1, |
| 69 | + wantErr: true, |
| 70 | + }, |
| 71 | + } |
| 72 | + for _, tt := range tests { |
| 73 | + t.Run(tt.name, func(t *testing.T) { |
| 74 | + container := LanguageContainer{ |
| 75 | + ReleaseInit: func(ctx context.Context, c *release.Config) (*message.ReleaseInitResponse, error) { |
| 76 | + if tt.wantErr { |
| 77 | + return nil, os.ErrNotExist |
| 78 | + } |
| 79 | + return &message.ReleaseInitResponse{}, nil |
| 80 | + }, |
| 81 | + } |
| 82 | + if gotCode := Run(tt.args, &container); gotCode != tt.wantCode { |
| 83 | + t.Errorf("Run() = %v, want %v", gotCode, tt.wantCode) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestRun_noArgs(t *testing.T) { |
| 90 | + defer func() { |
| 91 | + if r := recover(); r == nil { |
| 92 | + t.Errorf("The code did not panic") |
| 93 | + } |
| 94 | + }() |
| 95 | + Run([]string{}, &LanguageContainer{}) |
| 96 | +} |
| 97 | + |
| 98 | +func TestRun_ReleaseInitWritesResponse(t *testing.T) { |
| 99 | + tmpDir := t.TempDir() |
| 100 | + if err := os.WriteFile(filepath.Join(tmpDir, "release-init-request.json"), []byte("{}"), 0644); err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + args := []string{"release-init", "-librarian", tmpDir} |
| 104 | + want := &message.ReleaseInitResponse{Error: "test error"} |
| 105 | + container := LanguageContainer{ |
| 106 | + ReleaseInit: func(ctx context.Context, c *release.Config) (*message.ReleaseInitResponse, error) { |
| 107 | + return want, nil |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + if code := Run(args, &container); code != 0 { |
| 112 | + t.Errorf("Run() = %v, want 0", code) |
| 113 | + } |
| 114 | + |
| 115 | + responsePath := filepath.Join(tmpDir, "release-init-response.json") |
| 116 | + bytes, err := os.ReadFile(responsePath) |
| 117 | + if err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + got := &message.ReleaseInitResponse{} |
| 121 | + if err := json.Unmarshal(bytes, got); err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + if diff := cmp.Diff(want, got); diff != "" { |
| 125 | + t.Errorf("response mismatch (-want +got):\n%s", diff) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestRun_ReleaseInitReadsContextArgs(t *testing.T) { |
| 130 | + tmpDir := t.TempDir() |
| 131 | + librarianDir := filepath.Join(tmpDir, "librarian") |
| 132 | + if err := os.Mkdir(librarianDir, 0755); err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | + if err := os.WriteFile(filepath.Join(librarianDir, "release-init-request.json"), []byte("{}"), 0644); err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | + repoDir := filepath.Join(tmpDir, "repo") |
| 139 | + if err := os.Mkdir(repoDir, 0755); err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + outputDir := filepath.Join(tmpDir, "output") |
| 143 | + if err := os.Mkdir(outputDir, 0755); err != nil { |
| 144 | + t.Fatal(err) |
| 145 | + } |
| 146 | + args := []string{"release-init", "-librarian", librarianDir, "-repo", repoDir, "-output", outputDir} |
| 147 | + var gotConfig *release.Config |
| 148 | + container := LanguageContainer{ |
| 149 | + ReleaseInit: func(ctx context.Context, c *release.Config) (*message.ReleaseInitResponse, error) { |
| 150 | + gotConfig = c |
| 151 | + return &message.ReleaseInitResponse{}, nil |
| 152 | + }, |
| 153 | + } |
| 154 | + if code := Run(args, &container); code != 0 { |
| 155 | + t.Errorf("Run() = %v, want 0", code) |
| 156 | + } |
| 157 | + if got, want := gotConfig.Context.LibrarianDir, librarianDir; got != want { |
| 158 | + t.Errorf("gotConfig.Context.LibrarianDir = %q, want %q", got, want) |
| 159 | + } |
| 160 | + if got, want := gotConfig.Context.RepoDir, repoDir; got != want { |
| 161 | + t.Errorf("gotConfig.Context.RepoDir = %q, want %q", got, want) |
| 162 | + } |
| 163 | + if got, want := gotConfig.Context.OutputDir, outputDir; got != want { |
| 164 | + t.Errorf("gotConfig.Context.OutputDir = %q, want %q", got, want) |
| 165 | + } |
| 166 | +} |
0 commit comments