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

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

Merged
merged 7 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
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 .fluence dir
shamsartem marked this conversation as resolved.
Show resolved Hide resolved
# 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);
shamsartem marked this conversation as resolved.
Show resolved Hide resolved
}

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
);