Skip to content

Conversation

@jww3 jww3 requested a review from a team as a code owner May 26, 2023 14:26
@jww3 jww3 requested review from TingluoHuang and rentziass May 26, 2023 14:26
var heredocIndex = line.IndexOf("<<", StringComparison.Ordinal);

// Normal style NAME=VALUE
if (equalsIndex >= 0 && (heredocIndex < 0 || equalsIndex < heredocIndex))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I basically just swapped the order of this conditional and the else if that follows it.

output = endIndex > startIndex ? text.Substring(startIndex, endIndex - startIndex) : string.Empty;
}
// Normal style NAME=VALUE
else if (equalsIndex >= 0 && heredocIndex < 0)
Copy link
Contributor Author

@jww3 jww3 May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we can be stricter here:

Note that we're now checking:

if (equalsIndex >= 0 && heredocIndex < 0)

whereas before it was:

if (equalsIndex >= 0 && (heredocIndex < 0 || equalsIndex < heredocIndex))

Comment on lines -170 to -183
private void WriteContent(
string path,
List<string> content,
string newline = null)
{
if (string.IsNullOrEmpty(newline))
{
newline = Environment.NewLine;
}

var encoding = new UTF8Encoding(true); // Emit BOM
var contentStr = string.Join(newline, content);
File.WriteAllText(path, contentStr, encoding);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were four verbatim copies of this method floating around, so I defined it once-and-only-once in TestUtil.cs.

I also dropped the ability to pass an arbitrary string in as a newline. (Several tests were passing a single SPACE which was such a subtle difference that it made it really hard to decipher why two seemingly-identical tests would behave differently.)

The new version in TestUtil.cs accepts an enum for specifying the LineEnding style.

public enum LineEndingType
{
    Native,
    Linux   = 0x__0A,
    Windows = 0x0D0A
}

public static void WriteContent(
    string path, 
    IEnumerable<string> content, 
    LineEndingType lineEnding = LineEndingType.Native)

string content = "MY_OUTPUT<<EOF line one line two line three EOF";
TestUtil.WriteContent(stateFile, content);
var ex = Assert.Throws<Exception>(() => _saveStateFileCommand.ProcessCommand(_executionContext.Object, stateFile, null));
Assert.Contains("Matching delimiter not found", ex.Message);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned earlier, it wasn't easy to spot the difference between this test case and the one that begins on line 190 and I was puzzling over why that one is expected to succeed and this one is expected to fail.

The difference is just the third arg (" ") passed to WriteContent which "tricks" WriteContent into mashing all the content together on a single line.

I think it's clearer to just be explicit about that:

string content = "MY_OUTPUT<<EOF line one line two line three EOF";

Comment on lines -272 to -276
"MY_ENV<<=EOF",
"hello",
"one",
"=EOF",
"MY_ENV_2<<<EOF",
Copy link
Contributor Author

@jww3 jww3 May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't clear to me that the = on line 272 and the final < on line 276 are actually considered part of the "end marker", so I split these two cases out into a separate unit test, SetEnvFileCommand_Heredoc_EndMarkerVariations that leverages

[InlineData("=EOF")][InlineData("<EOF")]...

to make things clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next three files in this PR were nearly identical. I took all the duplicated code and moved it into this file. That allowed me to pare down the three files that follow.

Copy link
Member

@rentziass rentziass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for looking into it!

Copy link
Member

@Link- Link- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any glaring issues, let's move this forward!

@jww3 jww3 merged commit 4ffd081 into main Jun 29, 2023
@jww3 jww3 deleted the users/jww3/fix-multiline-envvar branch June 29, 2023 10:52
ashb pushed a commit to ashb/runner that referenced this pull request Jun 30, 2023
…e processing. (actions#2627)

* Fixed a bug where a misplaced `=` character could bypass heredoc-style processing.

Fixes https://github.com/github/c2c-actions/issues/6910

GitHub Docs for context:  https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings

* Consolidate near-identical FileCommand-related unit test classes. (actions#2672)
cory-miller added a commit that referenced this pull request Aug 16, 2023
cory-miller added a commit that referenced this pull request Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants