11// Copyright 2020-2022 The MathWorks, Inc.
22
33import * as exec from "@actions/exec" ;
4+ import * as io from "@actions/io" ;
45import * as toolCache from "@actions/tool-cache" ;
56import * as script from "./script" ;
67
78jest . mock ( "@actions/exec" ) ;
9+ jest . mock ( "@actions/io" ) ;
810jest . mock ( "@actions/tool-cache" ) ;
911
1012afterEach ( ( ) => {
@@ -56,6 +58,8 @@ describe("script downloader/runner", () => {
5658} ) ;
5759
5860describe ( "install command generator" , ( ) => {
61+ const whichMock = io . which as jest . Mock ;
62+
5963 const scriptPath = "hello.sh" ;
6064
6165 beforeAll ( ( ) => {
@@ -64,13 +68,20 @@ describe("install command generator", () => {
6468
6569 it ( "does not change the command on Windows" , ( ) => {
6670 const cmd = script . generateExecCommand ( "win32" , scriptPath ) ;
67- expect ( cmd ) . toEqual ( `bash ${ scriptPath } ` ) ;
71+ expect ( cmd ) . resolves . toEqual ( `bash ${ scriptPath } ` ) ;
6872 } ) ;
6973
7074 [ "darwin" , "linux" ] . forEach ( ( platform ) => {
7175 it ( `calls the command with sudo on ${ platform } ` , ( ) => {
76+ whichMock . mockResolvedValue ( "path/to/sudo" ) ;
77+ const cmd = script . generateExecCommand ( platform , scriptPath ) ;
78+ expect ( cmd ) . resolves . toEqual ( `sudo -E bash ${ scriptPath } ` ) ;
79+ } ) ;
80+
81+ it ( `calls the command without sudo on ${ platform } ` , ( ) => {
82+ whichMock . mockRejectedValue ( "No sudo!" ) ;
7283 const cmd = script . generateExecCommand ( platform , scriptPath ) ;
73- expect ( cmd ) . toEqual ( `sudo -E bash ${ scriptPath } ` ) ;
84+ expect ( cmd ) . resolves . toEqual ( `bash ${ scriptPath } ` ) ;
7485 } ) ;
7586 } ) ;
7687} ) ;
@@ -87,4 +98,4 @@ describe("default install root", () => {
8798 testCase ( "win32" , 'Program Files' ) ;
8899 testCase ( "darwin" , "opt" ) ;
89100 testCase ( "linux" , "opt" ) ;
90- } )
101+ } )
0 commit comments