|
| 1 | +/* |
| 2 | +Copyright 2026 The Cozystack 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 | + "slices" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +// A maintainer's run by hand continues the batch pull request the scheduled |
| 26 | +// run opened as its bot, with nothing set: it edits that pull request rather |
| 27 | +// than opening a second one for the line. |
| 28 | +func TestHandRunContinuesTheBotsBatch(t *testing.T) { |
| 29 | + for _, tc := range []struct { |
| 30 | + name, author string |
| 31 | + continued bool |
| 32 | + }{ |
| 33 | + {"the scheduled run's bot", "cozystack-ci[bot]", true}, |
| 34 | + {"another bot", "renovate[bot]", false}, |
| 35 | + } { |
| 36 | + t.Run(tc.name, func(t *testing.T) { |
| 37 | + f := newFixture(t) |
| 38 | + f.lines() |
| 39 | + f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"}) |
| 40 | + gh := &fakeGH{bot: batchBotLogin(func(string) (string, bool) { return "", false })} |
| 41 | + openBatch(t, f, gh) |
| 42 | + gh.prs[0].User.Login, gh.prs[0].User.Type = tc.author, "Bot" |
| 43 | + f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"}) |
| 44 | + f.now = f.now.AddDate(0, 0, 3) |
| 45 | + |
| 46 | + code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push") |
| 47 | + if code != 0 { |
| 48 | + t.Fatalf("run --continue = %d\n%s", code, stderr) |
| 49 | + } |
| 50 | + continued := slices.Equal(gh.edited, []string{"#90 fix(backport): batch for release-1.1, 2026-10-26"}) && len(gh.created) == 1 |
| 51 | + if continued != tc.continued { |
| 52 | + t.Errorf("edited %q, created %q; want the batch of %s continued=%v\n%s", gh.edited, gh.created, tc.author, tc.continued, stderr) |
| 53 | + } |
| 54 | + if !tc.continued && !strings.Contains(stderr, "ignoring #90") { |
| 55 | + t.Errorf("the run does not say it ignores #90:\n%s", stderr) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// The bot comes from BACKPORT_BATCH_BOT when it is set, empty included, and |
| 62 | +// is the scheduled run's own bot when it is not. |
| 63 | +func TestBatchBotLogin(t *testing.T) { |
| 64 | + env := func(v string, ok bool) func(string) (string, bool) { |
| 65 | + return func(key string) (string, bool) { |
| 66 | + if key != batchBotEnv { |
| 67 | + t.Errorf("looked up %s", key) |
| 68 | + } |
| 69 | + return v, ok |
| 70 | + } |
| 71 | + } |
| 72 | + for _, tc := range []struct { |
| 73 | + name, v string |
| 74 | + ok bool |
| 75 | + want string |
| 76 | + }{ |
| 77 | + {"unset", "", false, "cozystack-ci[bot]"}, |
| 78 | + {"set empty", "", true, ""}, |
| 79 | + {"set", "other-app[bot]", true, "other-app[bot]"}, |
| 80 | + } { |
| 81 | + if got := batchBotLogin(env(tc.v, tc.ok)); got != tc.want { |
| 82 | + t.Errorf("%s: batchBotLogin = %q, want %q", tc.name, got, tc.want) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments