|
| 1 | +import type { AnnotationProperties, ExitCode, InputOptions } from 'node_modules/@actions/core'; |
| 2 | +import { vi } from 'vitest'; |
| 3 | +import type { Mock } from 'vitest'; |
| 4 | + |
| 5 | +// Import the actual module |
| 6 | +const actual = await vi.importActual<typeof import('node_modules/@actions/core')>('@actions/core'); |
| 7 | + |
| 8 | +// Strictly typed MockedFunction helper that only applies to function types |
| 9 | +type MockedFunction<TFunc> = TFunc extends (...args: infer TArgs) => infer TReturn |
| 10 | + ? Mock<(...args: TArgs) => TReturn> & TFunc |
| 11 | + : never; |
| 12 | + |
| 13 | +/** |
| 14 | + * Writes a debug message to the user log. |
| 15 | + * @param message - The debug message to log. |
| 16 | + */ |
| 17 | +export const debug: MockedFunction<(message: string) => void> = vi.fn((message: string): void => {}); |
| 18 | + |
| 19 | +/** |
| 20 | + * Writes an informational message to the log. |
| 21 | + * @param message - The info message to log. |
| 22 | + */ |
| 23 | +export const info: MockedFunction<(message: string) => void> = vi.fn((message: string): void => {}); |
| 24 | + |
| 25 | +/** |
| 26 | + * Adds a warning issue with optional annotation properties. |
| 27 | + * @param message - The warning message or error. |
| 28 | + * @param properties - Optional properties to add to the annotation. |
| 29 | + */ |
| 30 | +export const warning: MockedFunction<(message: string | Error, properties?: AnnotationProperties) => void> = vi.fn( |
| 31 | + (message: string | Error, properties?: AnnotationProperties): void => {}, |
| 32 | +); |
| 33 | + |
| 34 | +/** |
| 35 | + * Adds a notice issue with optional annotation properties. |
| 36 | + * @param message - The notice message or error. |
| 37 | + * @param properties - Optional properties to add to the annotation. |
| 38 | + */ |
| 39 | +export const notice: MockedFunction<(message: string | Error, properties?: AnnotationProperties) => void> = vi.fn( |
| 40 | + (message: string | Error, properties?: AnnotationProperties): void => {}, |
| 41 | +); |
| 42 | + |
| 43 | +/** |
| 44 | + * Adds an error issue with optional annotation properties. |
| 45 | + * @param message - The error message or error. |
| 46 | + * @param properties - Optional properties to add to the annotation. |
| 47 | + */ |
| 48 | +export const error: MockedFunction<(message: string | Error, properties?: AnnotationProperties) => void> = vi.fn( |
| 49 | + (message: string | Error, properties?: AnnotationProperties): void => {}, |
| 50 | +); |
| 51 | + |
| 52 | +/** |
| 53 | + * Sets the action status to failed. |
| 54 | + * When the action exits it will be with an exit code of 1. |
| 55 | + * @param message - The error message or object. |
| 56 | + * @throws An error with the specified message. |
| 57 | + */ |
| 58 | +export const setFailed: MockedFunction<(message: string | Error) => void> = vi.fn((message: string | Error) => { |
| 59 | + actual.setFailed(message); |
| 60 | +}); |
| 61 | + |
| 62 | +/** |
| 63 | + * Begins a new output group. Output until the next `endGroup` will be foldable in this group. |
| 64 | + * @param name - The name of the output group. |
| 65 | + */ |
| 66 | +export const startGroup: MockedFunction<(name: string) => void> = vi.fn((name: string): void => {}); |
| 67 | + |
| 68 | +/** |
| 69 | + * Ends the current output group. |
| 70 | + */ |
| 71 | +export const endGroup: MockedFunction<() => void> = vi.fn((): void => {}); |
| 72 | + |
| 73 | +/** |
| 74 | + * Gets the value of an input. Will return an empty string if the input is not defined. |
| 75 | + * @param name - Name of the input to get |
| 76 | + * @param options - Optional. See InputOptions. |
| 77 | + * @returns string |
| 78 | + */ |
| 79 | +export const getInput: MockedFunction<(name: string, options?: InputOptions) => string> = vi.fn( |
| 80 | + (name: string, options?: InputOptions): string => { |
| 81 | + return actual.getInput(name, options); |
| 82 | + }, |
| 83 | +); |
| 84 | + |
| 85 | +/** |
| 86 | + * Gets the input value of the boolean type in the YAML 1.2 "core schema" specification. |
| 87 | + * Supported boolean input list: `true | True | TRUE | false | False | FALSE` |
| 88 | + * @param name - Name of the input to get |
| 89 | + * @param options - Optional. See InputOptions. |
| 90 | + * @returns boolean |
| 91 | + */ |
| 92 | +export const getBooleanInput: MockedFunction<(name: string, options?: InputOptions) => boolean> = vi.fn( |
| 93 | + (name: string, options?: InputOptions): boolean => { |
| 94 | + return actual.getBooleanInput(name, options); |
| 95 | + }, |
| 96 | +); |
| 97 | + |
| 98 | +/** |
| 99 | + * Gets the values of an multiline input. Each value will be trimmed. |
| 100 | + * @param name - Name of the input to get |
| 101 | + * @param options - Optional. See InputOptions. |
| 102 | + * @returns string[] |
| 103 | + */ |
| 104 | +export const getMultilineInput: MockedFunction<(name: string, options?: InputOptions) => string[]> = vi.fn( |
| 105 | + (name: string, options?: InputOptions): string[] => { |
| 106 | + return actual.getMultilineInput(name, options); |
| 107 | + }, |
| 108 | +); |
| 109 | + |
| 110 | +/** |
| 111 | + * Masks a value in the log. When the masked value appears in the log, it is replaced with asterisks. |
| 112 | + * @param secret - Value to mask |
| 113 | + */ |
| 114 | +export const setSecret: MockedFunction<(secret: string) => void> = vi.fn((secret: string): void => {}); |
| 115 | + |
| 116 | +/** |
| 117 | + * Prepends the given path to the PATH environment variable. |
| 118 | + * @param inputPath - Path to prepend |
| 119 | + */ |
| 120 | +export const addPath: MockedFunction<(inputPath: string) => void> = vi.fn((inputPath: string): void => {}); |
| 121 | + |
| 122 | +/** |
| 123 | + * Sets env variable for this action and future actions in the job. |
| 124 | + * @param name - Name of the variable to set |
| 125 | + * @param val - Value of the variable |
| 126 | + */ |
| 127 | +export const exportVariable: MockedFunction<(name: string, val: string) => void> = vi.fn( |
| 128 | + (name: string, val: string): void => {}, |
| 129 | +); |
| 130 | + |
| 131 | +/** |
| 132 | + * Enables or disables the echoing of commands into stdout for the rest of the step. |
| 133 | + * @param enabled - True to enable echoing, false to disable |
| 134 | + */ |
| 135 | +export const setCommandEcho: MockedFunction<(enabled: boolean) => void> = vi.fn((enabled: boolean): void => {}); |
| 136 | + |
| 137 | +/** |
| 138 | + * Begin an output group. |
| 139 | + * @param name - Name of the output group |
| 140 | + * @param fn - Function to execute within the output group |
| 141 | + */ |
| 142 | +export const group: MockedFunction<(name: string, fn: () => Promise<void>) => Promise<void>> = vi.fn( |
| 143 | + async (name: string, fn: () => Promise<void>): Promise<void> => { |
| 144 | + await fn(); |
| 145 | + }, |
| 146 | +); |
| 147 | + |
| 148 | +/** |
| 149 | + * Saves state for current action. |
| 150 | + * The state can only be retrieved by this action's post job execution. |
| 151 | + * @param name - Name of the state to store |
| 152 | + * @param value - Value to store. Non-string values will be converted to a string via JSON.stringify |
| 153 | + */ |
| 154 | +export const saveState: MockedFunction<(name: string, value: string) => void> = vi.fn( |
| 155 | + (name: string, value: string): void => {}, |
| 156 | +); |
| 157 | + |
| 158 | +/** |
| 159 | + * Gets the value of an state set by this action's main execution. |
| 160 | + * @param name - Name of the state to get |
| 161 | + * @returns string |
| 162 | + */ |
| 163 | +export const getState: MockedFunction<(name: string) => string> = vi.fn((name: string): string => ''); |
| 164 | + |
| 165 | +/** |
| 166 | + * Gets whether Actions Step Debug is on or not |
| 167 | + * @returns boolean |
| 168 | + */ |
| 169 | +export const isDebug: MockedFunction<() => boolean> = vi.fn((): boolean => false); |
| 170 | + |
| 171 | +/** |
| 172 | + * Gets the value of an OIDC token from the GitHub Actions runtime |
| 173 | + * @param audience - Optional audience for the token |
| 174 | + * @returns string |
| 175 | + */ |
| 176 | +export const getIDToken: MockedFunction<(audience?: string) => Promise<string>> = vi.fn( |
| 177 | + async (audience?: string): Promise<string> => '', |
| 178 | +); |
| 179 | + |
| 180 | +/** |
| 181 | + * Sets the value of an output. |
| 182 | + * @param name - Name of the output to set |
| 183 | + * @param value - Value to store. Non-string values will be converted to a string via JSON.stringify |
| 184 | + */ |
| 185 | +export const setOutput: MockedFunction<(name: string, value: string) => void> = vi.fn( |
| 186 | + (name: string, value: string): void => {}, |
| 187 | +); |
| 188 | + |
| 189 | +// Re-export types |
| 190 | +export type { InputOptions, AnnotationProperties }; |
| 191 | +export type { ExitCode }; |
0 commit comments