|
| 1 | +// Copyright 2023 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:auto_submit/action/git_cli_revert_method.dart'; |
| 8 | +import 'package:auto_submit/git/git_cli.dart'; |
| 9 | +import 'package:auto_submit/service/config.dart'; |
| 10 | +import 'package:auto_submit/service/github_service.dart'; |
| 11 | +import 'package:cocoon_server_test/test_logging.dart'; |
| 12 | +import 'package:github/src/common/model/pulls.dart'; |
| 13 | +import 'package:github/src/common/model/repos.dart'; |
| 14 | +import 'package:test/fake.dart'; |
| 15 | +import 'package:test/test.dart'; |
| 16 | + |
| 17 | +import '../requests/github_webhook_test_data.dart'; |
| 18 | + |
| 19 | +void main() { |
| 20 | + useTestLoggerPerTest(); |
| 21 | + |
| 22 | + late _FakeGitCli gitCli; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + gitCli = _FakeGitCli(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('createRevert uses the branch from the originating PR', () async { |
| 29 | + final config = _FakeConfig(); |
| 30 | + final method = GitCliRevertMethod(gitCli: gitCli); |
| 31 | + final revert = await method.createRevert( |
| 32 | + config, |
| 33 | + 'matanlurey', |
| 34 | + 'I said so', |
| 35 | + generatePullRequest( |
| 36 | + baseRef: 'flutter-3.32-candidate.0', |
| 37 | + mergeCommitSha: 'abc123', |
| 38 | + ), |
| 39 | + ); |
| 40 | + expect(gitCli.createBranch$newBranchName, 'revert_abc123'); |
| 41 | + expect(gitCli.setUpstream$branchName, 'revert_abc123'); |
| 42 | + expect(gitCli.pushBranch$branchName, 'revert_abc123'); |
| 43 | + expect(revert?.base?.ref, 'flutter-3.32-candidate.0'); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +final class _FakeConfig extends Fake implements Config { |
| 48 | + @override |
| 49 | + Future<String> generateGithubToken(RepositorySlug slug) async { |
| 50 | + return 'a_github_token'; |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + Future<GithubService> createGithubService(RepositorySlug slug) async { |
| 55 | + return _FakeGithubService(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +final class _FakeGithubService extends Fake implements GithubService { |
| 60 | + @override |
| 61 | + Future<Branch> getBranch(RepositorySlug slug, String branchName) async { |
| 62 | + return Branch(branchName, null); |
| 63 | + } |
| 64 | + |
| 65 | + @override |
| 66 | + Future<List<PullRequestReview>> getPullRequestReviews( |
| 67 | + RepositorySlug slug, |
| 68 | + int pullRequestNumber, |
| 69 | + ) async { |
| 70 | + return []; |
| 71 | + } |
| 72 | + |
| 73 | + @override |
| 74 | + Future<PullRequest> createPullRequest({ |
| 75 | + required RepositorySlug slug, |
| 76 | + String? title, |
| 77 | + String? head, |
| 78 | + required String base, |
| 79 | + bool draft = false, |
| 80 | + String? body, |
| 81 | + }) async { |
| 82 | + return PullRequest(base: PullRequestHead(ref: base)); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +final class _FakeGitCli extends Fake implements GitCli { |
| 87 | + @override |
| 88 | + Future<ProcessResult> cloneRepository({ |
| 89 | + required RepositorySlug slug, |
| 90 | + required String workingDirectory, |
| 91 | + required String targetDirectory, |
| 92 | + List<String>? options, |
| 93 | + bool throwOnError = true, |
| 94 | + }) async { |
| 95 | + return ProcessResult(0, 0, '', ''); |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + Future<ProcessResult> setupUserConfig({ |
| 100 | + required RepositorySlug slug, |
| 101 | + required String workingDirectory, |
| 102 | + bool throwOnError = true, |
| 103 | + }) async { |
| 104 | + return ProcessResult(0, 0, '', ''); |
| 105 | + } |
| 106 | + |
| 107 | + @override |
| 108 | + Future<ProcessResult> setupUserEmailConfig({ |
| 109 | + required RepositorySlug slug, |
| 110 | + required String workingDirectory, |
| 111 | + bool throwOnError = true, |
| 112 | + }) async { |
| 113 | + return ProcessResult(0, 0, '', ''); |
| 114 | + } |
| 115 | + |
| 116 | + late String createBranch$newBranchName; |
| 117 | + |
| 118 | + @override |
| 119 | + Future<ProcessResult> createBranch({ |
| 120 | + required String newBranchName, |
| 121 | + required String workingDirectory, |
| 122 | + bool useCheckout = false, |
| 123 | + bool throwOnError = true, |
| 124 | + }) async { |
| 125 | + createBranch$newBranchName = newBranchName; |
| 126 | + return ProcessResult(0, 0, '', ''); |
| 127 | + } |
| 128 | + |
| 129 | + late String setUpstream$branchName; |
| 130 | + |
| 131 | + @override |
| 132 | + Future<ProcessResult> setUpstream({ |
| 133 | + required RepositorySlug slug, |
| 134 | + required String workingDirectory, |
| 135 | + required String branchName, |
| 136 | + required String token, |
| 137 | + bool throwOnError = true, |
| 138 | + }) async { |
| 139 | + setUpstream$branchName = branchName; |
| 140 | + return ProcessResult(0, 0, '', ''); |
| 141 | + } |
| 142 | + |
| 143 | + @override |
| 144 | + Future<ProcessResult> revertChange({ |
| 145 | + required String commitSha, |
| 146 | + required String workingDirectory, |
| 147 | + bool throwOnError = true, |
| 148 | + }) async { |
| 149 | + return ProcessResult(0, 0, '', ''); |
| 150 | + } |
| 151 | + |
| 152 | + late String pushBranch$branchName; |
| 153 | + |
| 154 | + @override |
| 155 | + Future<ProcessResult> pushBranch({ |
| 156 | + required String branchName, |
| 157 | + required String workingDirectory, |
| 158 | + bool throwOnError = true, |
| 159 | + }) async { |
| 160 | + pushBranch$branchName = branchName; |
| 161 | + return ProcessResult(0, 0, '', ''); |
| 162 | + } |
| 163 | +} |
0 commit comments