diff --git a/devtools/generate_discovery_client.sh b/devtools/generate_discovery_client.sh index 18448a2..90f3d16 100755 --- a/devtools/generate_discovery_client.sh +++ b/devtools/generate_discovery_client.sh @@ -1,4 +1,17 @@ #!/bin/bash -e +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. # This script generates the "discovery" client for the GenerativeLanguage API. # It is needed for file upload, which GAPIC clients don't support. @@ -31,3 +44,7 @@ google-api-go-generator -api_json_file $docfile \ -internal_pkg github.com/google/generative-ai-go/genai/internal \ -gensupport_pkg github.com/google/generative-ai-go/genai/internal/gensupport +# Replace license with the proper one for this repo. +file=internal/generativelanguage/v1beta/generativelanguage-gen.go +cat license.txt <(tail +5 $file) | sponge $file + diff --git a/devtools/pre-push-hook.sh b/devtools/pre-push-hook.sh index 3a1cd3c..2321719 100755 --- a/devtools/pre-push-hook.sh +++ b/devtools/pre-push-hook.sh @@ -1,4 +1,17 @@ #!/bin/sh -e +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. # This script performs some checks. # Install as a pre-push hook from the repo root with: diff --git a/genai/internal/generativelanguage/v1beta/generativelanguage-gen.go b/genai/internal/generativelanguage/v1beta/generativelanguage-gen.go index 1163d73..0c42800 100644 --- a/genai/internal/generativelanguage/v1beta/generativelanguage-gen.go +++ b/genai/internal/generativelanguage/v1beta/generativelanguage-gen.go @@ -1,6 +1,16 @@ -// Copyright 2024 Google LLC. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. // Code generated file. DO NOT EDIT. diff --git a/genai/internal/gensupport/buffer.go b/genai/internal/gensupport/buffer.go index 3d0817e..a790ebc 100644 --- a/genai/internal/gensupport/buffer.go +++ b/genai/internal/gensupport/buffer.go @@ -1,6 +1,16 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/buffer_test.go b/genai/internal/gensupport/buffer_test.go index 8a99744..e87ad74 100644 --- a/genai/internal/gensupport/buffer_test.go +++ b/genai/internal/gensupport/buffer_test.go @@ -1,6 +1,16 @@ -// Copyright 2015 Google LLC -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/doc.go b/genai/internal/gensupport/doc.go index 752c4b4..9a60f2d 100644 --- a/genai/internal/gensupport/doc.go +++ b/genai/internal/gensupport/doc.go @@ -1,6 +1,16 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. // Package gensupport is an internal implementation detail used by code // generated by the google-api-go-generator tool. diff --git a/genai/internal/gensupport/error.go b/genai/internal/gensupport/error.go index 886c653..9cc3647 100644 --- a/genai/internal/gensupport/error.go +++ b/genai/internal/gensupport/error.go @@ -1,6 +1,16 @@ -// Copyright 2022 Google LLC. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/error_test.go b/genai/internal/gensupport/error_test.go index 4805af4..2ec6480 100644 --- a/genai/internal/gensupport/error_test.go +++ b/genai/internal/gensupport/error_test.go @@ -1,6 +1,16 @@ -// Copyright 2022 Google LLC. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/json.go b/genai/internal/gensupport/json.go index eab49a1..26d8c03 100644 --- a/genai/internal/gensupport/json.go +++ b/genai/internal/gensupport/json.go @@ -1,6 +1,16 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/json_test.go b/genai/internal/gensupport/json_test.go index e659ec7..f32aefc 100644 --- a/genai/internal/gensupport/json_test.go +++ b/genai/internal/gensupport/json_test.go @@ -1,6 +1,16 @@ -// Copyright 2015 Google LLC -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/jsonfloat.go b/genai/internal/gensupport/jsonfloat.go index 13c2f93..ed9c531 100644 --- a/genai/internal/gensupport/jsonfloat.go +++ b/genai/internal/gensupport/jsonfloat.go @@ -1,6 +1,16 @@ -// Copyright 2016 Google LLC. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/jsonfloat_test.go b/genai/internal/gensupport/jsonfloat_test.go index cbaf46b..fe2f6b6 100644 --- a/genai/internal/gensupport/jsonfloat_test.go +++ b/genai/internal/gensupport/jsonfloat_test.go @@ -1,6 +1,16 @@ -// Copyright 2016 Google LLC. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/media.go b/genai/internal/gensupport/media.go index 7bddd7a..7db2653 100644 --- a/genai/internal/gensupport/media.go +++ b/genai/internal/gensupport/media.go @@ -1,6 +1,16 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/media_test.go b/genai/internal/gensupport/media_test.go index 65fd62b..f42f52c 100644 --- a/genai/internal/gensupport/media_test.go +++ b/genai/internal/gensupport/media_test.go @@ -1,6 +1,16 @@ -// Copyright 2015 Google LLC -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/params.go b/genai/internal/gensupport/params.go index 5f31bc1..fecd8d4 100644 --- a/genai/internal/gensupport/params.go +++ b/genai/internal/gensupport/params.go @@ -1,6 +1,16 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/params_test.go b/genai/internal/gensupport/params_test.go index 8f6e021..b201ba8 100644 --- a/genai/internal/gensupport/params_test.go +++ b/genai/internal/gensupport/params_test.go @@ -1,6 +1,16 @@ -// Copyright 2021 Google LLC. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/resumable.go b/genai/internal/gensupport/resumable.go index 8edfc50..4268f09 100644 --- a/genai/internal/gensupport/resumable.go +++ b/genai/internal/gensupport/resumable.go @@ -1,6 +1,16 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/resumable_test.go b/genai/internal/gensupport/resumable_test.go index 0000d40..479832d 100644 --- a/genai/internal/gensupport/resumable_test.go +++ b/genai/internal/gensupport/resumable_test.go @@ -1,6 +1,16 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/retry.go b/genai/internal/gensupport/retry.go index 20b57d9..eee51c5 100644 --- a/genai/internal/gensupport/retry.go +++ b/genai/internal/gensupport/retry.go @@ -1,6 +1,16 @@ -// Copyright 2021 Google LLC. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/retryable_linux.go b/genai/internal/gensupport/retryable_linux.go index a916c3d..30ba0e0 100644 --- a/genai/internal/gensupport/retryable_linux.go +++ b/genai/internal/gensupport/retryable_linux.go @@ -1,6 +1,16 @@ -// Copyright 2020 Google LLC. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. //go:build linux // +build linux diff --git a/genai/internal/gensupport/send.go b/genai/internal/gensupport/send.go index f61b00f..b0596f6 100644 --- a/genai/internal/gensupport/send.go +++ b/genai/internal/gensupport/send.go @@ -1,6 +1,16 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/send_test.go b/genai/internal/gensupport/send_test.go index 5953440..9e2b343 100644 --- a/genai/internal/gensupport/send_test.go +++ b/genai/internal/gensupport/send_test.go @@ -1,6 +1,16 @@ -// Copyright 2017 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/util_test.go b/genai/internal/gensupport/util_test.go index bdfe14c..9ba6463 100644 --- a/genai/internal/gensupport/util_test.go +++ b/genai/internal/gensupport/util_test.go @@ -1,6 +1,16 @@ -// Copyright 2016 Google LLC -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/version.go b/genai/internal/gensupport/version.go index 23f6aa2..d8005f9 100644 --- a/genai/internal/gensupport/version.go +++ b/genai/internal/gensupport/version.go @@ -1,6 +1,16 @@ -// Copyright 2020 Google LLC. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/gensupport/version_test.go b/genai/internal/gensupport/version_test.go index cffeab7..3d66e7d 100644 --- a/genai/internal/gensupport/version_test.go +++ b/genai/internal/gensupport/version_test.go @@ -1,6 +1,16 @@ -// Copyright 2019 Google LLC. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package gensupport diff --git a/genai/internal/version.go b/genai/internal/version.go index e6f1736..7e3414e 100644 --- a/genai/internal/version.go +++ b/genai/internal/version.go @@ -1,6 +1,16 @@ -// Copyright 2022 Google LLC. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package internal diff --git a/license_test.go b/license_test.go new file mode 100644 index 0000000..194f67b --- /dev/null +++ b/license_test.go @@ -0,0 +1,164 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "bufio" + "errors" + "io/fs" + "os" + "path/filepath" + "regexp" + "strings" + "testing" + "unicode" +) + +var wantFile = filepath.Join("genai", "license.txt") + +func TestLicense(t *testing.T) { + lic, err := os.ReadFile(wantFile) + if err != nil { + t.Fatal(err) + } + want := string(lic) + want = eraseYear(want) + want = removeCommentPrefix(want, "//") + // Remove final blank line(s). + want = strings.TrimRightFunc(want, unicode.IsSpace) + + check := func(t *testing.T, file, prefix, contents string) { + t.Helper() + got := removeCommentPrefix(contents, prefix) + got = eraseYear(got) + if got != want { + t.Errorf("%s: bad license: does not match contents of %s", file, wantFile) + t.Logf("got %q", got) + t.Logf("want %q", want) + } + } + + t.Run("scripts", func(t *testing.T) { + shellScripts, err := globTree(".", "*.sh") + if err != nil { + t.Fatal(err) + } + for _, f := range shellScripts { + got, err := topComment(f, "#") + if err != nil { + t.Fatal(err) + } + // Remove shbang line. + if strings.HasPrefix(got, "#!") { + if i := strings.IndexByte(got, '\n'); i > 0 { + got = got[i+1:] + } + } + check(t, f, "#", got) + } + }) + t.Run("go source", func(t *testing.T) { + goFiles, err := globTree(".", "*.go") + if err != nil { + t.Fatal(err) + } + for _, f := range goFiles { + got, err := topComment(f, "//") + if err != nil { + t.Fatal(err) + } + check(t, f, "//", got) + } + }) +} + +var yearRegexp = regexp.MustCompile(`[Cc]opyright \d\d\d\d`) + +func eraseYear(s string) string { + return yearRegexp.ReplaceAllLiteralString(s, "Copyright YYYY") +} + +func removeCommentPrefix(s, prefix string) string { + var lines []string + for _, line := range strings.Split(s, "\n") { + lines = append(lines, strings.TrimPrefix(line, prefix)) + } + return strings.Join(lines, "\n") +} + +// topComment returns the comment at the top of the file, up to the first blank or non-comment line. +// Exception: the first comment contains "generated", in which case we take the second one. +func topComment(file, commentPrefix string) (string, error) { + f, err := os.Open(file) + if err != nil { + return "", err + } + defer f.Close() + scan := bufio.NewScanner(f) + var lines []string + gen := false + n := 0 + for scan.Scan() { + line := scan.Text() + n++ + if n == 1 && strings.Contains(line, "generated") { + gen = true + continue + } + if gen && line == "" { + gen = false + continue + } + if !gen { + if strings.HasPrefix(line, commentPrefix) { + lines = append(lines, line) + } else { + break + } + } + } + if scan.Err() != nil { + return "", scan.Err() + } + return strings.Join(lines, "\n"), nil +} + +// globTree runs filepath.Glob on dir and all its subdirectories, recursively. +// The filenames it returns begin with dir. +// The pattern must not contain path separators. +func globTree(dir, pattern string) ([]string, error) { + if strings.ContainsRune(pattern, filepath.Separator) { + return nil, errors.New("pattern contains path separator") + } + + // Check for bad pattern. + if _, err := filepath.Match(pattern, ""); err != nil { + return nil, err + } + var paths []string + err := filepath.WalkDir(dir, func(path string, _ fs.DirEntry, err error) error { + if err != nil { + return err + } + if ok, _ := filepath.Match(pattern, filepath.Base(path)); ok { + paths = append(paths, path) + } + return nil + }) + if err != nil { + return nil, err + } + return paths, nil +}