Skip to content

Commit 99a12a4

Browse files
authored
Re-setting working directory if given (Azure#257)
* Re-setting working directory if given * changing yaml * address cr
1 parent 2f07635 commit 99a12a4

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

azure-functions-language-worker-protobuf/src/proto/FunctionRpc.proto

+2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ message WorkerStatusResponse {
189189
message FunctionEnvironmentReloadRequest {
190190
// Environment variables from the current process
191191
map<string, string> environment_variables = 1;
192+
// Current directory of function app
193+
string function_app_directory = 2;
192194
}
193195

194196
message FunctionEnvironmentReloadResponse {

azure-pipelines.yml

-11
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,6 @@ jobs:
7575
AzureWebJobsCosmosDBConnectionString: $(AzureWebJobsCosmosDBConnectionString)
7676
FUNCTIONS_WORKER_RUNTIME: 'node'
7777
languageWorkers:node:workerDirectory: $(System.DefaultWorkingDirectory)
78-
- task: CopyFiles@2
79-
inputs:
80-
SourceFolder: '$(System.DefaultWorkingDirectory)/test/testResults'
81-
Contents: '*.trx'
82-
TargetFolder: '$(Build.ArtifactStagingDirectory)'
83-
- task: PublishBuildArtifacts@1
84-
inputs:
85-
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
86-
ArtifactName: 'testResults'
87-
publishLocation: 'Container'
88-
displayName: 'Run tests and publish test result artifacts'
8978
9079
- job: BuildArtifacts
9180
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))

src/WorkerChannel.ts

+10
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ export class WorkerChannel implements IWorkerChannel {
257257
let error = null;
258258
try {
259259
process.env = Object.assign({}, msg.environmentVariables);
260+
// Change current working directory
261+
if (msg.functionAppDirectory)
262+
{
263+
this.log({
264+
message: `Changing current working directory to ${msg.functionAppDirectory}`,
265+
level: LogLevel.Information,
266+
logCategory: LogCategory.System
267+
});
268+
process.chdir(msg.functionAppDirectory);
269+
}
260270
} catch (e)
261271
{
262272
error = e;

test/WorkerChannelTests.ts

+61-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ describe('WorkerChannel', () => {
9393
environmentVariables: {
9494
"hello": "world",
9595
"SystemDrive": "Q:"
96-
}
96+
},
97+
functionAppDirectory: null
9798
}
9899
});
99100
sinon.assert.calledWith(stream.written, <rpc.IStreamingMessage>{
@@ -115,7 +116,8 @@ describe('WorkerChannel', () => {
115116
stream.addTestMessage({
116117
requestId: 'id',
117118
functionEnvironmentReloadRequest: {
118-
environmentVariables: {}
119+
environmentVariables: {},
120+
functionAppDirectory: null
119121
}
120122
});
121123
sinon.assert.calledWith(stream.written, <rpc.IStreamingMessage>{
@@ -134,7 +136,8 @@ describe('WorkerChannel', () => {
134136
stream.write({
135137
requestId: 'id',
136138
functionEnvironmentReloadRequest: {
137-
environmentVariables: {}
139+
environmentVariables: {},
140+
functionAppDirectory: null
138141
}
139142
});
140143
}).to.not.throw();
@@ -150,12 +153,66 @@ describe('WorkerChannel', () => {
150153
stream.write({
151154
requestId: 'id',
152155
functionEnvironmentReloadRequest: {
153-
environmentVariables: null
156+
environmentVariables: null,
157+
functionAppDirectory: null
154158
}
155159
});
156160
}).to.not.throw();
157161
});
158162

163+
it ('reloads environment variable and keeps cwd without functionAppDirectory', () => {
164+
let cwd = process.cwd();
165+
stream.addTestMessage({
166+
requestId: 'id',
167+
functionEnvironmentReloadRequest: {
168+
environmentVariables: {
169+
"hello": "world",
170+
"SystemDrive": "Q:"
171+
},
172+
functionAppDirectory: null
173+
}
174+
});
175+
sinon.assert.calledWith(stream.written, <rpc.IStreamingMessage>{
176+
requestId: 'id',
177+
functionEnvironmentReloadResponse: {
178+
result: {
179+
status: rpc.StatusResult.Status.Success
180+
}
181+
}
182+
});
183+
expect(process.env.hello).to.equal("world");
184+
expect(process.env.SystemDrive).to.equal("Q:");
185+
expect(process.cwd() == cwd);
186+
});
187+
188+
it ('reloads environment variable and changes functionAppDirectory', () => {
189+
let cwd = process.cwd();
190+
let newDir = "/";
191+
stream.addTestMessage({
192+
requestId: 'id',
193+
functionEnvironmentReloadRequest: {
194+
environmentVariables: {
195+
"hello": "world",
196+
"SystemDrive": "Q:"
197+
},
198+
functionAppDirectory: newDir
199+
}
200+
});
201+
sinon.assert.calledWith(stream.written, <rpc.IStreamingMessage>{
202+
requestId: 'id',
203+
functionEnvironmentReloadResponse: {
204+
result: {
205+
status: rpc.StatusResult.Status.Success
206+
}
207+
}
208+
});
209+
expect(process.env.hello).to.equal("world");
210+
expect(process.env.SystemDrive).to.equal("Q:");
211+
expect(process.cwd() != newDir);
212+
expect(process.cwd() == newDir);
213+
process.chdir(cwd);
214+
});
215+
159216
it ('invokes function', () => {
160217
loader.getFunc.returns((context) => context.done());
161218
loader.getInfo.returns({

0 commit comments

Comments
 (0)