@@ -16,6 +16,114 @@ describe("kiotaClient", () => {
16
16
sandbox . restore ( ) ;
17
17
} ) ;
18
18
19
+ describe ( "setKiotaBinaryPath" , ( ) => {
20
+ let originalPkg : any ;
21
+
22
+ beforeEach ( ( ) => {
23
+ originalPkg = ( process as any ) . pkg ;
24
+ } ) ;
25
+
26
+ afterEach ( ( ) => {
27
+ if ( originalPkg !== undefined ) {
28
+ ( process as any ) . pkg = originalPkg ;
29
+ } else {
30
+ delete ( process as any ) . pkg ;
31
+ }
32
+ delete process . env . KIOTA_BINARY_PATH ;
33
+ } ) ;
34
+
35
+ it ( "should set binary location from KIOTA_BINARY_PATH environment variable" , async ( ) => {
36
+ process . env . KIOTA_BINARY_PATH = "/custom/path/to/kiota" ;
37
+ delete ( process as any ) . pkg ;
38
+
39
+ const setKiotaConfigStub = sinon . stub ( ) . resolves ( ) ;
40
+ const searchDescriptionStub = sinon . stub ( ) . resolves ( { } ) ;
41
+
42
+ const { searchOpenAPISpec } = proxyquire ( "../../src/common/kiotaClient" , {
43
+ "@microsoft/kiota" : {
44
+ setKiotaConfig : setKiotaConfigStub ,
45
+ searchDescription : searchDescriptionStub ,
46
+ "@noCallThru" : true ,
47
+ } ,
48
+ } ) ;
49
+
50
+ await searchOpenAPISpec ( "test-query" ) ;
51
+
52
+ assert ( setKiotaConfigStub . calledOnce ) ;
53
+ assert ( setKiotaConfigStub . calledWith ( { binaryLocation : "/custom/path/to/kiota" } ) ) ;
54
+ } ) ;
55
+
56
+ it ( "should set binary location to kiota-bin directory when running inside pkg" , async ( ) => {
57
+ delete process . env . KIOTA_BINARY_PATH ;
58
+ ( process as any ) . pkg = { } ;
59
+
60
+ const setKiotaConfigStub = sinon . stub ( ) . resolves ( ) ;
61
+ const searchDescriptionStub = sinon . stub ( ) . resolves ( { } ) ;
62
+
63
+ const { searchOpenAPISpec } = proxyquire ( "../../src/common/kiotaClient" , {
64
+ "@microsoft/kiota" : {
65
+ setKiotaConfig : setKiotaConfigStub ,
66
+ searchDescription : searchDescriptionStub ,
67
+ "@noCallThru" : true ,
68
+ } ,
69
+ path : {
70
+ join : sinon . stub ( ) . returns ( "/home/user/kiota-bin" ) ,
71
+ "@noCallThru" : true ,
72
+ } ,
73
+ os : {
74
+ homedir : sinon . stub ( ) . returns ( "/home/user" ) ,
75
+ "@noCallThru" : true ,
76
+ } ,
77
+ } ) ;
78
+
79
+ await searchOpenAPISpec ( "test-query" ) ;
80
+
81
+ assert ( setKiotaConfigStub . calledOnce ) ;
82
+ assert ( setKiotaConfigStub . calledWith ( { binaryLocation : "/home/user/kiota-bin" } ) ) ;
83
+ } ) ;
84
+
85
+ it ( "should not call setKiotaConfig when not in pkg and no env var set" , async ( ) => {
86
+ delete process . env . KIOTA_BINARY_PATH ;
87
+ delete ( process as any ) . pkg ;
88
+
89
+ const setKiotaConfigStub = sinon . stub ( ) . resolves ( ) ;
90
+ const searchDescriptionStub = sinon . stub ( ) . resolves ( { } ) ;
91
+
92
+ const { searchOpenAPISpec } = proxyquire ( "../../src/common/kiotaClient" , {
93
+ "@microsoft/kiota" : {
94
+ setKiotaConfig : setKiotaConfigStub ,
95
+ searchDescription : searchDescriptionStub ,
96
+ "@noCallThru" : true ,
97
+ } ,
98
+ } ) ;
99
+
100
+ await searchOpenAPISpec ( "test-query" ) ;
101
+
102
+ assert ( setKiotaConfigStub . notCalled ) ;
103
+ } ) ;
104
+
105
+ it ( "should prioritize KIOTA_BINARY_PATH over pkg detection" , async ( ) => {
106
+ process . env . KIOTA_BINARY_PATH = "/env/path/to/kiota" ;
107
+ ( process as any ) . pkg = { } ;
108
+
109
+ const setKiotaConfigStub = sinon . stub ( ) . resolves ( ) ;
110
+ const searchDescriptionStub = sinon . stub ( ) . resolves ( { } ) ;
111
+
112
+ const { searchOpenAPISpec } = proxyquire ( "../../src/common/kiotaClient" , {
113
+ "@microsoft/kiota" : {
114
+ setKiotaConfig : setKiotaConfigStub ,
115
+ searchDescription : searchDescriptionStub ,
116
+ "@noCallThru" : true ,
117
+ } ,
118
+ } ) ;
119
+
120
+ await searchOpenAPISpec ( "test-query" ) ;
121
+
122
+ assert ( setKiotaConfigStub . calledOnce ) ;
123
+ assert ( setKiotaConfigStub . calledWith ( { binaryLocation : "/env/path/to/kiota" } ) ) ;
124
+ } ) ;
125
+ } ) ;
126
+
19
127
it ( "happy path: searchOpenAPISpec" , async ( ) => {
20
128
const mockSearchResult = {
21
129
"api-spec" : {
0 commit comments