blob: 0fa73f1534c533c7f22a37c98f9e865ea6217ffc [file] [log] [blame]
Rob Mohrf9e555b2024-05-06 18:15:10 +00001# Copyright 2024 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Full test of bazel module's update_commit_hash() method."""
15
Rob Mohr758a4722024-05-08 22:03:16 +000016from __future__ import annotations
Rob Mohrf9e555b2024-05-06 18:15:10 +000017
18import dataclasses
Rob Mohr758a4722024-05-08 22:03:16 +000019from typing import Generator, TYPE_CHECKING
20
Rob Mohrf9e555b2024-05-06 18:15:10 +000021from PB.recipe_modules.pigweed.bazel.tests.update_commit_hash import (
22 UpdateCommitHashProperties,
23)
24from PB.recipe_modules.pigweed.checkout.options import (
25 Options as CheckoutOptions,
26)
Rob Mohr758a4722024-05-08 22:03:16 +000027from recipe_engine import post_process
28
29if TYPE_CHECKING: # pragma: no cover
30 from recipe_engine import config_types, recipe_test_api
Rob Mohrf9e555b2024-05-06 18:15:10 +000031
32DEPS = [
33 'pigweed/bazel',
34 'pigweed/checkout',
Rob Mohr4b28bcb2024-06-05 21:37:13 +000035 'recipe_engine/file',
Rob Mohrf9e555b2024-05-06 18:15:10 +000036 'recipe_engine/path',
37 'recipe_engine/properties',
Rob Mohr62be9882024-06-05 21:46:50 +000038 'recipe_engine/step',
Rob Mohrf9e555b2024-05-06 18:15:10 +000039]
40
41PROPERTIES = UpdateCommitHashProperties
42
43
44@dataclasses.dataclass
45class _FakeCheckoutContext:
46 root: config_types.Path
47
48
49def RunSteps(api, props):
50 options = CheckoutOptions(remote="https://pigweed.googlesource.com/super")
51 checkout = api.checkout(options)
52
53 # For coverage.
54 path: config_types.Path | None = None
55 if 'bar' in props.project_remote:
56 path = api.path.start_dir / 'WORKSPACE'
57
58 update_result = api.bazel.update_commit_hash(
59 checkout=checkout,
60 project_remote=props.project_remote,
61 new_revision='ffffffffffffffffffffffffffffffffffffffff',
62 path=path,
63 )
64
Rob Mohr62be9882024-06-05 21:46:50 +000065 if update_result:
66 step = api.step.empty('update result')
67 step.presentation.step_summary_text = repr(update_result)
68
Rob Mohrf9e555b2024-05-06 18:15:10 +000069
70def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]:
71 def properties(**kwargs):
72 return api.properties(UpdateCommitHashProperties(**kwargs))
73
74 def _url(x: str = 'pigweed/pigweed'):
75 assert ':' not in x
76 return f'https://pigweed.googlesource.com/{x}'
77
78 yield api.test(
79 'success',
80 properties(project_remote=_url('pigweed/pigweed')),
Rob Mohr62be9882024-06-05 21:46:50 +000081 api.post_process(post_process.MustRun, 'update result'),
Rob Mohrf9e555b2024-05-06 18:15:10 +000082 )
83
84 yield api.test(
85 'remote-not-found',
86 properties(project_remote=_url('bar')),
87 api.post_process(post_process.MustRunRE, r'could not find remote.*'),
88 api.post_process(post_process.DropExpectation),
Rob Mohr62be9882024-06-05 21:46:50 +000089 api.post_process(post_process.DoesNotRun, 'update result'),
Rob Mohrf9e555b2024-05-06 18:15:10 +000090 )
91
92 yield api.test(
93 'commit-not-found',
94 properties(project_remote=_url('other/repo')),
95 api.post_process(post_process.MustRunRE, r'could not find commit.*'),
96 api.post_process(post_process.DropExpectation),
Rob Mohr62be9882024-06-05 21:46:50 +000097 api.post_process(post_process.DoesNotRun, 'update result'),
Rob Mohrf9e555b2024-05-06 18:15:10 +000098 )
Rob Mohr4b28bcb2024-06-05 21:37:13 +000099
100 yield api.test(
101 'multiple',
102 properties(project_remote=_url('pigweed/pigweed')),
103 api.step_data(
104 'read old WORKSPACE',
105 api.file.read_text(api.bazel.MULTIPLE_ROLL_WORKSPACE_FILE),
106 ),
107 )