Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ACNA-894 - Action running in local cannot be debugged #129

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,31 @@ 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
shazron marked this conversation as resolved.
Show resolved Hide resolved
delete env.AIO__RUNTIME_NAMESPACE
delete env.AIO__RUNTIME_APIHOST
const newCredentials = {
RUNTIME_NAMESPACE: namespace,
RUNTIME_AUTH: auth,
RUNTIME_APIHOST: apihost
}

// remove old keys (match by normalized key name)
for (const key in env) {
// match AIO_ or AIO__ since they map to the same key
// see https://github.com/adobe/aio-lib-core-config/issues/49
const match = key.match(/^AIO__(.+)/i) || key.match(/^AIO_(.+)/i)
if (match) {
for (const newCredential in newCredentials) {
if (newCredential.toLowerCase() === match[1].toLowerCase()) {
delete env[key]
}
}
}
}

// set the new keys
for (const key in newCredentials) {
env[`AIO_${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
4 changes: 4 additions & 0 deletions test/__fixtures__/app-env/my-env.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FOO=bar
AIO_RUNTIME_NAMESPACE=local-namespace
AIO_RUNTIME_AUTH=local-auth
AIO_RUNTIME_APIHOST=local-apihost
7 changes: 7 additions & 0 deletions test/__fixtures__/app-env/my-env.original
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FOO=bar
AIO_runtime_namespace=my-remote-namespace
AIO_runtime_auth=my-remote-auth
AIO_runtime_apihost=my-remote-apihost
AIO__runtime_namespace=my-remote-namespace2
AIO__runtime_auth=my-remote-auth2
AIO__runtime_apihost=my-remote-apihost2
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