Skip to content

Commit

Permalink
Add support for advanced flow.json service key types (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed May 24, 2024
1 parent 1b630d2 commit eceb9d6
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 110 deletions.
10 changes: 10 additions & 0 deletions .changeset/big-crews-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@onflow/flow-js-testing": minor
---

Allow loading service key from environment variables and files.

**BREAKING CHANGES**

- `getConfigValue` and `set` have been removed as these were just a confusing abstraction above the `@onflow/config` packages
- They have been replaced by exporting they `config` instance directly from the package
15 changes: 2 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"dependencies": {
"@onflow/fcl": "1.3.2",
"@onflow/fcl-config": "^0.0.1",
"@onflow/flow-cadut": "^0.3.0-stable-cadence.1",
"elliptic": "^6.5.4",
"esm": "^3.2.25",
Expand Down
77 changes: 0 additions & 77 deletions src/config.js

This file was deleted.

1 change: 1 addition & 0 deletions src/emulator/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Logger extends EventEmitter {
super(options)
this.handleMessage = this.handleMessage.bind(this)
this.process = null
this.setMaxListeners(100)
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/flow-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Flow JS Testing
*
* Copyright 2020-2021 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import path from "path"
import fs from "fs"

const TARGET = "flow.json"
let configPath = null
let config = null

function isDir(dir) {
return fs.lstatSync(dir).isDirectory()
}

function listFiles(dir) {
return new Set(fs.readdirSync(dir))
}

function parentDir(dir) {

Check warning on line 34 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return path.dirname(dir)

Check warning on line 35 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

function findTarget(dir) {
if (!isDir(dir)) throw new Error(`Not a directory: ${dir}`)

Check warning on line 39 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 39 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return listFiles(dir).has(TARGET) ? path.resolve(dir, TARGET) : null

Check warning on line 40 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

export function getConfigPath(dir) {
if (configPath != null) return configPath

Check warning on line 44 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 44 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const filePath = findTarget(dir)
if (filePath == null) {
if (dir === parentDir(dir)) {
throw new Error("No flow.json found")

Check warning on line 49 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 50 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 50 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return getConfigPath(parentDir(dir))

Check warning on line 51 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 52 in src/flow-config.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

configPath = filePath
return configPath
}

export function flowConfig() {
if (config != null) return config

const filePath = getConfigPath(process.cwd())
const content = fs.readFileSync(filePath, "utf8")
config = JSON.parse(content)

return config
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

export {init} from "./init"
export {set, getConfigValue} from "./config"
export {config} from "@onflow/fcl"
export {
getTemplate,
getScriptCode,
Expand Down
49 changes: 42 additions & 7 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* limitations under the License.
*/

import {set} from "./config"
import {config} from "@onflow/fcl"
import {flowConfig, getConfigPath} from "./flow-config"
import path from "path"
import fs from "fs"

const DEFAULT_COMPUTE_LIMIT = 9999

/**
* Inits framework variables, storing private key of service account and base path
Expand All @@ -30,12 +35,42 @@ export const init = async (basePath, props = {}) => {
pkey = "48a1f554aeebf6bf9fe0d7b5b79d080700b073ee77909973ea0b2f6fbc902",
} = props

set("PRIVATE_KEY", process.env.PK, "accounts/emulator-account/key", pkey)
set(
const cfg = flowConfig()

config().put("PRIVATE_KEY", getServiceKey(cfg) ?? pkey)

Check warning on line 40 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
config().put(
"SERVICE_ADDRESS",
process.env.SERVICE_ADDRESS,
"accounts/emulator-account/address",
"f8d6e0586b0a20c7"
cfg?.accounts?.["emulator-account"]?.address ?? "f8d6e0586b0a20c7"

Check warning on line 43 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
)
set("BASE_PATH", process.env.BASE_PATH, "testing/paths", basePath)
config().put("BASE_PATH", cfg?.testing?.paths ?? basePath)
config().put("fcl.limit", DEFAULT_COMPUTE_LIMIT)
}

function getServiceKey(cfg) {
const value = cfg?.accounts?.["emulator-account"]?.key
if (value) {
if (typeof value === "object") {
switch (value.type) {
case "hex":
return value.privateKey

Check warning on line 55 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 55 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
case "file": {
const configDir = path.dirname(getConfigPath())

Check warning on line 57 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const resovledPath = path.resolve(configDir, value.location)

Check warning on line 58 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return fs.readFileSync(resovledPath, "utf8")

Check warning on line 59 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 60 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
default:
return null

Check warning on line 62 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 62 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

Check warning on line 63 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} else if (typeof value === "string") {
if (value.startsWith("$")) {
return process.env[value.slice(1)]

Check warning on line 66 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} else {
return value
}

Check warning on line 69 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
} else {
return null

Check warning on line 71 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 72 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 72 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

return null

Check warning on line 75 in src/init.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
3 changes: 1 addition & 2 deletions src/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import * as fcl from "@onflow/fcl"
import {resolveArguments} from "@onflow/flow-cadut"
import {DEFAULT_COMPUTE_LIMIT} from "./config"
import {authorization} from "./crypto"
import emulator from "./emulator/emulator"
import {getTransactionCode, getScriptCode, defaultsByName} from "./file"
Expand Down Expand Up @@ -66,7 +65,7 @@ export const extractParameters = ixType => {
}

// Check that limit is always set
ixLimit = ixLimit || DEFAULT_COMPUTE_LIMIT
ixLimit = ixLimit || (await fcl.config().get("fcl.limit"))

if (ixName) {
const getIxTemplate =
Expand Down
9 changes: 0 additions & 9 deletions test/basic/config.test.js

This file was deleted.

0 comments on commit eceb9d6

Please sign in to comment.