Skip to content

Commit

Permalink
feat(tests): add env variable for user's fluence dir path [fixes DXJ-228
Browse files Browse the repository at this point in the history
] (#112)

* feat(tests): add env variable for user's fluence dir path [fixes DXJ-228]

* fix: set absolute path instead of a relative one

* feat: remove setup for rust toolchain

* feat: return rust toolchain setup

* remove workflow input, set path dynamically with github.workspace

* rename env variable

* update env variable description
  • Loading branch information
shamsartem authored Jan 19, 2023
1 parent 8f0b001 commit 9193992
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ jobs:
- name: Run tests
env:
FLUENCE_ENV: "${{ inputs.fluence-env }}"
FLUENCE_USER_DIR: "${{ github.workspace }}/tmp/.fluence"
CARGO_REGISTRIES_FLUENCE_INDEX: "git://crates.fluence.dev/index"
CARGO_REGISTRIES_FLUENCE_TOKEN: "${{ steps.secrets.outputs.CARGO_REGISTRIES_FLUENCE_TOKEN }}"
NPM_CONFIG_REGISTRY: "https://npm.fluence.dev"
Expand Down
3 changes: 3 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ FLUENCE_ENV="local"
# To turn on Countly debugger
DEBUG_COUNTLY="true"

# To set path for the user's .fluence dir. Default: ~/.fluence (for Windows: %LOCALAPPDATA%\fluence)
# FLUENCE_USER_DIR="/absolute/path/to/users/.fluence"

# example of cargo env variables
CARGO_REGISTRIES_FLUENCE_INDEX="git://crates.fluence.dev/index"
CARGO_REGISTRIES_FLUENCE_TOKEN="<token>"
Expand Down
7 changes: 6 additions & 1 deletion src/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/

import { Network } from "../src/lib/multiaddr";
import { DEBUG_COUNTLY, FLUENCE_ENV } from "../src/lib/setupEnvironment";
import {
DEBUG_COUNTLY,
FLUENCE_ENV,
FLUENCE_USER_DIR,
} from "../src/lib/setupEnvironment";

export type FluenceEnv = Network | "local";

Expand All @@ -24,6 +28,7 @@ declare global {
interface ProcessEnv {
[FLUENCE_ENV]: FluenceEnv;
[DEBUG_COUNTLY]: "true" | "false";
[FLUENCE_USER_DIR]: string | undefined;
}
}
}
20 changes: 15 additions & 5 deletions src/lib/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
VSCODE_DIR_NAME,
} from "./const";
import { recursivelyFindFile } from "./helpers/recursivelyFindFile";
import { FLUENCE_USER_DIR } from "./setupEnvironment";

export const validatePath = async (path: string): Promise<string | true> => {
try {
Expand All @@ -65,12 +66,21 @@ export const ensureDir = async (dirPath: string): Promise<string> => {

// User .fluence paths:

export const ensureUserFluenceDir = async (
export const ensureUserFluenceDir = (
commandObj: CommandObj
): Promise<string> =>
commandObj.config.windows
? ensureDir(commandObj.config.configDir)
: ensureDir(path.join(os.homedir(), FLUENCE_DIR_NAME));
): Promise<string> => {
const globalFluenceDirPathFromEnv = process.env[FLUENCE_USER_DIR];

if (typeof globalFluenceDirPathFromEnv === "string") {
return ensureDir(globalFluenceDirPathFromEnv);
}

if (commandObj.config.windows) {
return ensureDir(commandObj.config.configDir);
}

return ensureDir(path.join(os.homedir(), FLUENCE_DIR_NAME));
};

export const getUserCountlyDir = async (
commandObj: CommandObj
Expand Down
12 changes: 12 additions & 0 deletions src/lib/setupEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import path from "node:path";

import dotenv from "dotenv";

import type { FluenceEnv } from "../environment.d";
Expand All @@ -22,6 +24,7 @@ import { NETWORKS } from "./multiaddr";

export const FLUENCE_ENV = "FLUENCE_ENV";
export const DEBUG_COUNTLY = "DEBUG_COUNTLY";
export const FLUENCE_USER_DIR = "FLUENCE_USER_DIR";

dotenv.config();

Expand All @@ -48,6 +51,9 @@ const resolveEnvVariable = <T>(
const isTrueOrFalseString = (v: unknown): v is "true" | "false" =>
v === "true" || v === "false";

const isAbsolutePath = (v: unknown): v is string =>
typeof v === "string" && path.isAbsolute(v);

process.env[FLUENCE_ENV] = resolveEnvVariable(
FLUENCE_ENV,
(v): v is FluenceEnv => NETWORKS.some((n) => n === v) || v === "local",
Expand All @@ -59,3 +65,9 @@ process.env[DEBUG_COUNTLY] = resolveEnvVariable(
isTrueOrFalseString,
"false"
);

process.env[FLUENCE_USER_DIR] = resolveEnvVariable(
FLUENCE_USER_DIR,
isAbsolutePath,
undefined
);

0 comments on commit 9193992

Please sign in to comment.