Skip to content

Commit

Permalink
fix: ACNA-894 - Action running in local cannot be debugged
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron committed Aug 24, 2020
1 parent 77c47f6 commit e45a6a3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,29 @@ function saveAndReplaceDotEnvCredentials (dotenvFile, saveFile, apihost, namespa
fs.moveSync(dotenvFile, saveFile)
// Only override needed env vars and preserve other vars in .env
const env = dotenv.parse(fs.readFileSync(saveFile))
env.AIO_RUNTIME_APIHOST = apihost
env.AIO_RUNTIME_AUTH = auth
env.AIO_RUNTIME_NAMESPACE = namespace
// existing AIO__ vars might override above AIO_ vars
delete env.AIO__RUNTIME_AUTH
delete env.AIO__RUNTIME_NAMESPACE
delete env.AIO__RUNTIME_APIHOST
const newCredentials = {
AIO_RUNTIME_NAMESPACE: namespace,
AIO_RUNTIME_AUTH: auth,
AIO_RUNTIME_APIHOST: apihost
}

// remove old keys (match by normalized key name)
for (const key in env) {
const match = key.match(/^AIO_(.+)/i)
if (match) {
for (const newCredential in newCredentials) {
if (newCredential.toLowerCase() === key.toLowerCase()) {
delete env[key]
}
}
}
}

// set the new keys
for (const key in newCredentials) {
env[key] = newCredentials[key]
}

const envContent = Object.keys(env).reduce((content, k) => content + `${k}=${env[k]}\n`, '')

fs.writeFileSync(dotenvFile, envContent)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"devDependencies": {
"@types/hapi__joi": "^16.0.2",
"@types/jest": "^25.1.0",
"eol": "^0.9.1",
"eslint": "^6.5.1",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/app-env/my-env.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AIO_RUNTIME_NAMESPACE=local-namespace
AIO_RUNTIME_AUTH=local-auth
AIO_RUNTIME_APIHOST=local-apihost
3 changes: 3 additions & 0 deletions test/__fixtures__/app-env/my-env.original
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AIO_runtime_namespace=my-remote-namespace
AIO_runtime_auth=my-remote-auth
AIO_runtime_apihost=my-remote-apihost
28 changes: 28 additions & 0 deletions test/lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/
const { vol } = global.mockFs()
const fs = require('fs')
const eol = require('eol')

const utils = require('../../lib/utils')
let mockResult = jest.fn()
Expand Down Expand Up @@ -258,5 +260,31 @@ describe('lib/utils.zip', () => {
})
})

test('saveAndReplaceDotEnvCredentials', () => {
global.loadFs(vol, 'app-env')

const localCredentials = {
APIHOST: 'local-apihost',
NAMESPACE: 'local-namespace',
AUTH: 'local-auth'
}

const envFile = 'my-env.original'
const envBackupFile = 'my-env.save'
const envExpectedOutputFixture = 'my-env.expected'

expect(
() => utils.saveAndReplaceDotEnvCredentials(envFile, envBackupFile, localCredentials.APIHOST, localCredentials.NAMESPACE, localCredentials.AUTH)
).not.toThrowError()

expect(fs.existsSync(envFile)).toEqual(true)
expect(fs.existsSync(envBackupFile)).toEqual(true)

const envFileContents = fs.readFileSync(envFile).toString()
const expectedEnvFileContents = fs.readFileSync(envExpectedOutputFixture).toString()

expect(eol.auto(envFileContents)).toEqual(eol.auto(expectedEnvFileContents))
})

// todo test utils independently + mock utils in scripts once it is exposed as a library
// for now we test most of utils through scripts

0 comments on commit e45a6a3

Please sign in to comment.