Skip to content

Commit

Permalink
fix: env func (#12662)
Browse files Browse the repository at this point in the history
* fix: env func

* test: ut
  • Loading branch information
yuqizhou77 authored Nov 6, 2024
1 parent 2a6d177 commit 9aa2f03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/fx-core/src/component/utils/envFunctionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async function readFileContent(
fromPath: string
): Promise<Result<string, FxError>> {
const ext = path.extname(filePath);
if (ext.toLowerCase() !== ".txt") {
if (ext.toLowerCase() !== ".txt" && ext.toLowerCase() !== ".md") {
ctx.logProvider.error(
getLocalizedString("core.envFunc.unsupportedFile.errorLog", filePath, "txt")
);
Expand All @@ -150,7 +150,8 @@ async function readFileContent(
try {
let fileContent = await fs.readFile(absolutePath, "utf8");
fileContent = stripBom(fileContent);
const processedFileContent = expandEnvironmentVariable(fileContent, envs);
let processedFileContent = expandEnvironmentVariable(fileContent, envs);
processedFileContent = processedFileContent.replace(/\r\n/g, "\n");
return ok(processedFileContent);
} catch (e) {
ctx.logProvider.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ describe("expandVariableWithFunction", async () => {
[FeatureFlagName.EnvFileFunc]: "true",
});
const content =
"description:\"$[file('testfile1.txt')]\",description2:\"$[file( file( 'C://testfile2.txt' ))] $[file(${{FILE_PATH}})]\"";
"description:\"$[file('testfile1.md')]\",description2:\"$[file( file( 'C://testfile2.txt' ))] $[file(${{FILE_PATH}})]\"";
sandbox.stub(fs, "pathExists").resolves(true);
sandbox.stub(fs, "readFile").callsFake((file: number | fs.PathLike) => {
if (file.toString().endsWith("testfile1.txt")) {
return Promise.resolve("description in ${{TEST_ENV}}" as any);
} else if (file.toString().endsWith("testfile2.txt")) {
return Promise.resolve("test/testfile1.txt" as any);
}
if (file.toString().endsWith("testfile1.md")) {
return Promise.resolve("description in ${{TEST_ENV}}" as any);
} else {
throw new Error("not support " + file);
}
Expand Down Expand Up @@ -124,7 +127,7 @@ describe("expandVariableWithFunction", async () => {
FILE_PATH: "testfile1.txt",
[FeatureFlagName.EnvFileFunc]: "true",
});
const content = "description:\"$[ file('testfile1.md')]\"C://test";
const content = "description:\"$[ file('testfile1.png')]\"C://test";
const res = await expandVariableWithFunction(
content,
context as any,
Expand Down

0 comments on commit 9aa2f03

Please sign in to comment.