Skip to content

Commit

Permalink
fix: additional '>' char generated in command shell prompt when gcl -…
Browse files Browse the repository at this point in the history
…-no-color (#1375)
  • Loading branch information
ANGkeith authored Oct 15, 2024
1 parent d0be1c3 commit 503a0e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {CICDVariable} from "./variables-from-files.js";
import {GitlabRunnerCPUsPresetValue, GitlabRunnerMemoryPresetValue, GitlabRunnerPresetValues} from "./gitlab-preset.js";

const CI_PROJECT_DIR = "/gcl-builds";
const GCL_SHELL_PROMPT_PLACEHOLDER = "<gclShellPromptPlaceholder>";
interface JobOptions {
argv: Argv;
writeStreams: WriteStreams;
Expand Down Expand Up @@ -610,11 +611,11 @@ export class Job {
private generateScriptCommands (scripts: string[]) {
let cmd = "";
scripts.forEach((script) => {
// Print command echo'ed in color
// Print command echo'ed with $GCL_SHELL_PROMPT_PLACEHOLDER
const split = script.split(/\r?\n/);
const multilineText = split.length > 1 ? " # collapsed multi-line command" : "";
const text = split[0]?.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/[$]/g, "\\$");
cmd += chalk`echo "{green $ ${text}${multilineText}}"\n`;
cmd += `echo "${GCL_SHELL_PROMPT_PLACEHOLDER} ${text}${multilineText}"\n`;

// Execute actual script
cmd += `${script}\n`;
Expand Down Expand Up @@ -860,7 +861,11 @@ export class Job {
const outFunc = (line: string, stream: (txt: string) => void, colorize: (str: string) => string) => {
this.refreshLongRunningSilentTimeout(writeStreams);
stream(`${this.formattedJobName} `);
if (!line.startsWith("\u001b[32m$")) {
if (line.startsWith(GCL_SHELL_PROMPT_PLACEHOLDER)) {
// replace the GCL_SHELL_PROMPT_PLACEHOLDER with `$` and make the SHELL_PROMPT line green color
line = line.slice(GCL_SHELL_PROMPT_PLACEHOLDER.length);
line = chalk`{green $${line}}`;
} else {
stream(`${colorize(">")} `);
}
stream(`${line}\n`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {handler} from "../../../src/handler.js";
import chalk from "chalk";
import {initSpawnSpy} from "../../mocks/utils.mock.js";
import {WhenStatics} from "../../mocks/when-statics.js";
import {stripAnsi} from "../../utils.js";
import {Job} from "../../../src/job.js";
import path from "path";
import fs from "fs-extra";
Expand Down Expand Up @@ -112,14 +111,15 @@ describe("predefined-variables", () => {
cwd: cwd,
job: ["test-job"],
shellIsolation: true,
noColor: true,
}, writeStreams);

let expected = "";
Object.keys(envVars).forEach(key => {
expected += `test-job > ${key}=${envVars[key]}\n`;
});

const filteredStdout = stripAnsi(writeStreams.stdoutLines.filter(f => stripAnsi(f).startsWith("test-job > ")).join("\n"));
const filteredStdout = writeStreams.stdoutLines.filter(f => f.startsWith("test-job > ")).join("\n");
expect(filteredStdout).toEqual(expected.trim());
expect(jobIdSpy).toHaveBeenCalledTimes(2);
expect(dateSpy).toHaveBeenCalledTimes(3);
Expand All @@ -135,6 +135,7 @@ CI_SERVER_SHELL_SSH_PORT: 8022
await handler({
cwd: cwd,
job: ["test-job"],
noColor: true,
}, writeStreams);

envVars["CI_API_V4_URL"] = "https://gitlab.com:8443/api/v4";
Expand All @@ -150,7 +151,7 @@ CI_SERVER_SHELL_SSH_PORT: 8022
Object.keys(envVars).forEach(key => {
expected += `test-job > ${key}=${envVars[key]}\n`;
});
const filteredStdout = stripAnsi(writeStreams.stdoutLines.filter(f => stripAnsi(f).startsWith("test-job > ")).join("\n"));
const filteredStdout = writeStreams.stdoutLines.filter(f => f.startsWith("test-job > ")).join("\n");
expect(filteredStdout).toEqual(expected.trim());
expect(jobIdSpy).toHaveBeenCalledTimes(2);
expect(dateSpy).toHaveBeenCalledTimes(3);
Expand All @@ -170,6 +171,7 @@ CI_SERVER_SHELL_SSH_PORT: 8022
"CI_SERVER_PORT=9443",
"CI_SERVER_SHELL_SSH_PORT=9022",
],
noColor: true,
}, writeStreams);

envVars["CI_API_V4_URL"] = "https://gitlab.com:9443/api/v4";
Expand All @@ -186,7 +188,7 @@ CI_SERVER_SHELL_SSH_PORT: 8022
expected += `test-job > ${key}=${envVars[key]}\n`;
});

const filteredStdout = stripAnsi(writeStreams.stdoutLines.filter(f => stripAnsi(f).startsWith("test-job > ")).join("\n"));
const filteredStdout = writeStreams.stdoutLines.filter(f => f.startsWith("test-job > ")).join("\n");
expect(filteredStdout).toEqual(expected.trim());
expect(jobIdSpy).toHaveBeenCalledTimes(2);
expect(dateSpy).toHaveBeenCalledTimes(3);
Expand Down

0 comments on commit 503a0e9

Please sign in to comment.