11jest . mock ( "fs" ) ;
22
3+ import { resolve } from "path" ;
34import fs from "fs" ;
45
56import { getPrivateKey } from "../src" ;
@@ -32,7 +33,10 @@ describe("getPrivateKey", () => {
3233 it ( "{ filepath } option" , ( ) => {
3334 const result = getPrivateKey ( { filepath : "test.pem" } ) ;
3435 expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
35- expect ( readFileSync ) . toHaveBeenCalledWith ( "test.pem" , "utf-8" ) ;
36+ expect ( readFileSync ) . toHaveBeenCalledWith (
37+ resolve ( process . cwd ( ) , "test.pem" ) ,
38+ "utf-8"
39+ ) ;
3640 expect ( result ) . toEqual ( "test.pem content" ) ;
3741 } ) ;
3842
@@ -52,10 +56,15 @@ describe("getPrivateKey", () => {
5256 } ,
5357 } ) ;
5458 expect ( existsSync ) . toHaveBeenCalledTimes ( 1 ) ;
55- expect ( existsSync ) . toHaveBeenCalledWith ( "test.pem" ) ;
59+ expect ( existsSync ) . toHaveBeenCalledWith (
60+ resolve ( process . cwd ( ) , "test.pem" )
61+ ) ;
5662
5763 expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
58- expect ( readFileSync ) . toHaveBeenCalledWith ( "test.pem" , "utf-8" ) ;
64+ expect ( readFileSync ) . toHaveBeenCalledWith (
65+ resolve ( process . cwd ( ) , "test.pem" ) ,
66+ "utf-8"
67+ ) ;
5968 expect ( result ) . toEqual ( "test.pem content" ) ;
6069 } ) ;
6170
@@ -65,15 +74,21 @@ describe("getPrivateKey", () => {
6574 env : { PRIVATE_KEY } ,
6675 } ) ;
6776 expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
68- expect ( readFileSync ) . toHaveBeenCalledWith ( "test.pem" , "utf-8" ) ;
77+ expect ( readFileSync ) . toHaveBeenCalledWith (
78+ resolve ( process . cwd ( ) , "test.pem" ) ,
79+ "utf-8"
80+ ) ;
6981 expect ( result ) . toEqual ( "test.pem content" ) ;
7082 } ) ;
7183
7284 it ( "single test.pem file in current working directory" , ( ) => {
7385 readdirSync . mockReturnValue ( [ "test.pem" ] ) ;
7486 const result = getPrivateKey ( ) ;
7587 expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
76- expect ( readFileSync ) . toHaveBeenCalledWith ( "test.pem" , "utf-8" ) ;
88+ expect ( readFileSync ) . toHaveBeenCalledWith (
89+ resolve ( process . cwd ( ) , "test.pem" ) ,
90+ "utf-8"
91+ ) ;
7792 expect ( result ) . toEqual ( "test.pem content" ) ;
7893 } ) ;
7994
@@ -83,6 +98,14 @@ describe("getPrivateKey", () => {
8398 `[@probot/get-private-key] More than one file found: \"test1.pem, test2.pem\". Set { filepath } option or set one of the environment variables: PRIVATE_KEY, PRIVATE_KEY_PATH`
8499 ) ;
85100 } ) ;
101+
102+ it ( "{ cwd }" , ( ) => {
103+ const result = getPrivateKey ( {
104+ cwd : "/app/current" ,
105+ } ) ;
106+ expect ( result ) . toEqual ( null ) ;
107+ expect ( readdirSync ) . toHaveBeenCalledWith ( "/app/current" ) ;
108+ } ) ;
86109 } ) ;
87110
88111 describe ( "with environment variables" , ( ) => {
@@ -111,10 +134,15 @@ describe("getPrivateKey", () => {
111134 process . env . PRIVATE_KEY_PATH = "test.pem" ;
112135 const result = getPrivateKey ( ) ;
113136 expect ( existsSync ) . toHaveBeenCalledTimes ( 1 ) ;
114- expect ( existsSync ) . toHaveBeenCalledWith ( "test.pem" ) ;
137+ expect ( existsSync ) . toHaveBeenCalledWith (
138+ resolve ( process . cwd ( ) , "test.pem" )
139+ ) ;
115140
116141 expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
117- expect ( readFileSync ) . toHaveBeenCalledWith ( "test.pem" , "utf-8" ) ;
142+ expect ( readFileSync ) . toHaveBeenCalledWith (
143+ resolve ( process . cwd ( ) , "test.pem" ) ,
144+ "utf-8"
145+ ) ;
118146 expect ( result ) . toEqual ( "test.pem content" ) ;
119147 } ) ;
120148
0 commit comments