mirror of
				https://github.com/actions-rs/toolchain.git
				synced 2025-10-31 16:53:53 +00:00 
			
		
		
		
	
							parent
							
								
									88dc235639
								
							
						
					
					
						commit
						e741d0b121
					
				| @ -62,6 +62,37 @@ describe("actions-rs/toolchain", () => { | ||||
|         expect(args.name).toBe("1.39.0"); | ||||
|     }); | ||||
| 
 | ||||
|     it("uses new style rust-toolchain file if input does not exist", function () { | ||||
|         const rustToolchainFile = tempWriteSync( | ||||
|             '[toolchain]\nchannel = "1.51.0"\ncomponents = [ "rustfmt", "clippy" ]\nprofile = "minimal"\ntargets = [ "wasm32-unknown-unknown", "thumbv2-none-eabi" ]' | ||||
|         ); | ||||
| 
 | ||||
|         const args = morph(() => { | ||||
|             return getToolchainArgs(rustToolchainFile); | ||||
|         }, {}); | ||||
| 
 | ||||
|         expect(args.name).toBe("1.51.0"); | ||||
|         expect(args.components).toStrictEqual(["rustfmt", "clippy"]); | ||||
|         expect(args.profile).toBe("minimal"); | ||||
|         expect(args.target).toBe("wasm32-unknown-unknown"); | ||||
|     }); | ||||
| 
 | ||||
|     it("uses new style rust-toolchain file with toml ending if input and rust-toolchain file does not exist", function () { | ||||
|         const rustToolchainFile = tempWriteSync( | ||||
|             '[toolchain]\nchannel = "1.51.0"\ncomponents = [ "rustfmt", "clippy" ]\nprofile = "minimal"\ntargets = [ "wasm32-unknown-unknown", "thumbv2-none-eabi" ]', | ||||
|             "./rust-toolchain.toml" | ||||
|         ); | ||||
| 
 | ||||
|         const args = morph(() => { | ||||
|             return getToolchainArgs(rustToolchainFile.replace(/\.toml^/, "")); | ||||
|         }, {}); | ||||
| 
 | ||||
|         expect(args.name).toBe("1.51.0"); | ||||
|         expect(args.components).toStrictEqual(["rustfmt", "clippy"]); | ||||
|         expect(args.profile).toBe("minimal"); | ||||
|         expect(args.target).toBe("wasm32-unknown-unknown"); | ||||
|     }); | ||||
| 
 | ||||
|     it("trims content of the override file", function () { | ||||
|         const rustToolchainFile = tempWriteSync("\n     1.39.0\n\n\n\n"); | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										2
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										5
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										5
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -764,6 +764,11 @@ | ||||
|                 } | ||||
|             } | ||||
|         }, | ||||
|         "@iarna/toml": { | ||||
|             "version": "2.2.5", | ||||
|             "resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.5.tgz", | ||||
|             "integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==" | ||||
|         }, | ||||
|         "@istanbuljs/load-nyc-config": { | ||||
|             "version": "1.1.0", | ||||
|             "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", | ||||
|  | ||||
| @ -35,7 +35,8 @@ | ||||
|         "@actions-rs/core": "^0.1.6", | ||||
|         "@actions/core": "^1.2.6", | ||||
|         "@actions/exec": "^1.0.4", | ||||
|         "@actions/io": "^1.0.2" | ||||
|         "@actions/io": "^1.0.2", | ||||
|         "@iarna/toml": "^2.2.5" | ||||
|     }, | ||||
|     "devDependencies": { | ||||
|         "@types/jest": "^26.0.15", | ||||
|  | ||||
							
								
								
									
										54
									
								
								src/args.ts
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								src/args.ts
									
									
									
									
									
								
							| @ -1,52 +1,36 @@ | ||||
| import { input } from "@actions-rs/core"; | ||||
| import { debug } from "@actions/core"; | ||||
| import { existsSync, readFileSync } from "fs"; | ||||
| import { parseToolchainFile } from "./toolchain_file"; | ||||
| 
 | ||||
| export interface ToolchainOptions { | ||||
|     name: string; | ||||
|     target: string | undefined; | ||||
|     default: boolean; | ||||
|     override: boolean; | ||||
|     profile: string | undefined; | ||||
|     components: string[] | undefined; | ||||
| } | ||||
| 
 | ||||
| function determineToolchain(overrideFile: string): string { | ||||
|     const toolchainInput = input.getInput("toolchain", { required: false }); | ||||
| 
 | ||||
|     if (toolchainInput) { | ||||
|         debug(`using toolchain from input: ${toolchainInput}`); | ||||
|         return toolchainInput; | ||||
|     } | ||||
| 
 | ||||
|     if (!existsSync(overrideFile)) { | ||||
|         throw new Error( | ||||
|             "toolchain input was not given and repository does not have a rust-toolchain file" | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     const rustToolchainFile = readFileSync(overrideFile, { | ||||
|         encoding: "utf-8", | ||||
|         flag: "r", | ||||
|     }).trim(); | ||||
| 
 | ||||
|     debug(`using toolchain from rust-toolchain file: ${rustToolchainFile}`); | ||||
| 
 | ||||
|     return rustToolchainFile; | ||||
|     target?: string | undefined; | ||||
|     default?: boolean; | ||||
|     override?: boolean; | ||||
|     profile?: string | undefined; | ||||
|     components?: string[] | undefined; | ||||
| } | ||||
| 
 | ||||
| export function getToolchainArgs(overrideFile: string): ToolchainOptions { | ||||
|     const toolchainInput = input.getInput("toolchain", { required: false }); | ||||
|     let components: string[] | undefined = input.getInputList("components"); | ||||
|     if (components && components.length === 0) { | ||||
|         components = undefined; | ||||
|     } | ||||
| 
 | ||||
|     const toolchainFromFile = parseToolchainFile(overrideFile); | ||||
| 
 | ||||
|     if (!toolchainInput && !toolchainFromFile) { | ||||
|         throw new Error( | ||||
|             "toolchain input was not given and repository does not have a rust-toolchain file" | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     return { | ||||
|         name: determineToolchain(overrideFile), | ||||
|         target: input.getInput("target") || undefined, | ||||
|         name: toolchainInput || toolchainFromFile?.name || "", | ||||
|         target: input.getInput("target") || toolchainFromFile?.target, | ||||
|         default: input.getInputBool("default"), | ||||
|         override: input.getInputBool("override"), | ||||
|         profile: input.getInput("profile") || undefined, | ||||
|         components: components, | ||||
|         profile: input.getInput("profile") || toolchainFromFile?.profile, | ||||
|         components: components || toolchainFromFile?.components, | ||||
|     }; | ||||
| } | ||||
|  | ||||
							
								
								
									
										60
									
								
								src/toolchain_file.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/toolchain_file.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,60 @@ | ||||
| import TOML from "@iarna/toml"; | ||||
| import { ToolchainOptions } from "./args"; | ||||
| import { existsSync, readFileSync } from "fs"; | ||||
| import { debug } from "@actions/core"; | ||||
| 
 | ||||
| interface RustToolchainFileToolchain { | ||||
|     channel: string; | ||||
|     targets: string[] | undefined; | ||||
|     profile: string | undefined; | ||||
|     components: string[] | undefined; | ||||
| } | ||||
| 
 | ||||
| interface RustToolchainFile { | ||||
|     toolchain: RustToolchainFileToolchain; | ||||
| } | ||||
| 
 | ||||
| export function parseToolchainFile( | ||||
|     overrideFile: string | ||||
| ): ToolchainOptions | null { | ||||
|     if (!existsSync(overrideFile)) { | ||||
|         if (existsSync(`${overrideFile}.toml`)) { | ||||
|             overrideFile = `${overrideFile}.toml`; | ||||
|         } else { | ||||
|             debug("Repository does not have a rust-toolchain file"); | ||||
| 
 | ||||
|             return null; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     const rustToolchainFile = readFileSync(overrideFile, { | ||||
|         encoding: "utf-8", | ||||
|         flag: "r", | ||||
|     }).trim(); | ||||
| 
 | ||||
|     try { | ||||
|         debug(`using toolchain from rust-toolchain file: ${rustToolchainFile}`); | ||||
| 
 | ||||
|         // eslint-disable-next-line
 | ||||
|         const parsedToolchain = (TOML.parse( | ||||
|             rustToolchainFile | ||||
|         ) as unknown) as RustToolchainFile; | ||||
| 
 | ||||
|         return { | ||||
|             name: parsedToolchain.toolchain.channel, | ||||
|             target: parsedToolchain.toolchain.targets?.[0], | ||||
|             profile: parsedToolchain.toolchain.profile, | ||||
|             components: parsedToolchain.toolchain.components, | ||||
|         }; | ||||
|     } catch (err) { | ||||
|         debug( | ||||
|             `using toolchain from old style rust-toolchain file: ${rustToolchainFile}` | ||||
|         ); | ||||
| 
 | ||||
|         return { | ||||
|             name: rustToolchainFile, | ||||
|             default: true, | ||||
|             override: false, | ||||
|         }; | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user