Skip to content

Commit

Permalink
Rename ChangeOwner to UserAccount (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthalman authored Dec 16, 2020
1 parent 33fcc02 commit fff59e1
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static IEnumerable<object[]> ParseTestInput()
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "id",
token => ValidateAggregate<UserAccount>(token, "id",
token => ValidateLiteral(token, "id"))),
token => ValidateWhitespace(token, " "),
token => ValidateLiteral(token, "src"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public abstract class FileTransferInstructionTests<TInstruction>
{
private readonly string instructionName;
private readonly Func<string, char, TInstruction> parse;
private readonly Func<IEnumerable<string>, string, ChangeOwner, string, char, TInstruction> create;
private readonly Func<IEnumerable<string>, string, UserAccount, string, char, TInstruction> create;

public FileTransferInstructionTests(
string instructionName,
Func<string, char, TInstruction> parse,
Func<IEnumerable<string>, string, ChangeOwner, string, char, TInstruction> create)
Func<IEnumerable<string>, string, UserAccount, string, char, TInstruction> create)
{
this.instructionName = instructionName;
this.parse = parse;
Expand Down Expand Up @@ -93,19 +93,19 @@ void Validate(TInstruction instruction, string user)
Assert.Equal($"{instructionName} --chown={user} src dst", instruction.ToString());
}

ChangeOwner changeOwner = new ChangeOwner("user");
UserAccount changeOwner = new UserAccount("user");
TInstruction instruction = this.create(new string[] { "src" }, "dst", changeOwner, null, Dockerfile.DefaultEscapeChar);
Validate(instruction, "user");

instruction.ChangeOwner = new ChangeOwner("user2");
instruction.ChangeOwner = new UserAccount("user2");
Validate(instruction, "user2");

instruction.ChangeOwner = null;
Assert.Null(instruction.ChangeOwner);
Assert.Equal($"{instructionName} src dst", instruction.ToString());

instruction = this.parse($"{instructionName}`\n src dst", '`');
instruction.ChangeOwner = new ChangeOwner("user");
instruction.ChangeOwner = new UserAccount("user");
Assert.Equal("user", instruction.ChangeOwner.User);
Assert.Equal($"{instructionName} --chown=user`\n src dst", instruction.ToString());

Expand All @@ -118,7 +118,7 @@ void Validate(TInstruction instruction, string user)
[Fact]
public void ChangeOwner_ResolveVariable()
{
ChangeOwner changeOwner = new ChangeOwner("$var");
UserAccount changeOwner = new UserAccount("$var");
TInstruction instruction = this.create(new string[] { "src" }, "dst", changeOwner, null, Dockerfile.DefaultEscapeChar);

Assert.Collection(instruction.Tokens, new Action<Token>[]
Expand All @@ -130,7 +130,7 @@ public void ChangeOwner_ResolveVariable()
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "$var",
token => ValidateAggregate<UserAccount>(token, "$var",
token => ValidateAggregate<LiteralToken>(token, "$var",
token => ValidateAggregate<VariableRefToken>(token, "$var",
token => ValidateString(token, "var"))))),
Expand All @@ -157,7 +157,7 @@ public void ChangeOwner_ResolveVariable()
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "user",
token => ValidateAggregate<UserAccount>(token, "user",
token => ValidateLiteral(token, "user"))),
token => ValidateWhitespace(token, " "),
token => ValidateLiteral(token, "src"),
Expand Down Expand Up @@ -258,7 +258,7 @@ public static IEnumerable<object[]> ParseTestInput(string instructionName)
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "1:2",
token => ValidateAggregate<UserAccount>(token, "1:2",
token => ValidateLiteral(token, "1"),
token => ValidateSymbol(token, ':'),
token => ValidateLiteral(token, "2"))),
Expand Down Expand Up @@ -314,7 +314,7 @@ public static IEnumerable<object[]> ParseTestInput(string instructionName)
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "1:2",
token => ValidateAggregate<UserAccount>(token, "1:2",
token => ValidateLiteral(token, "1"),
token => ValidateSymbol(token, ':'),
token => ValidateLiteral(token, "2"))),
Expand Down Expand Up @@ -360,7 +360,7 @@ public static IEnumerable<object[]> ParseTestInput(string instructionName)
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "1:2",
token => ValidateAggregate<UserAccount>(token, "1:2",
token => ValidateLiteral(token, "1"),
token => ValidateSymbol(token, ':'),
token => ValidateLiteral(token, "2"))),
Expand Down Expand Up @@ -591,7 +591,7 @@ public static IEnumerable<object[]> CreateTestInput(string instructionName)
"src2"
},
Destination = "dst",
ChangeOwner = new ChangeOwner("user", "group"),
ChangeOwner = new UserAccount("user", "group"),
TokenValidators = new Action<Token>[]
{
token => ValidateKeyword(token, instructionName),
Expand All @@ -601,7 +601,7 @@ public static IEnumerable<object[]> CreateTestInput(string instructionName)
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "user:group",
token => ValidateAggregate<UserAccount>(token, "user:group",
token => ValidateLiteral(token, "user"),
token => ValidateSymbol(token, ':'),
token => ValidateLiteral(token, "group"))),
Expand Down Expand Up @@ -649,7 +649,7 @@ public static IEnumerable<object[]> CreateTestInput(string instructionName)
},
Destination = "dst",
Permissions = "777",
ChangeOwner = new ChangeOwner("user", "group"),
ChangeOwner = new UserAccount("user", "group"),
TokenValidators = new Action<Token>[]
{
token => ValidateKeyword(token, instructionName),
Expand All @@ -659,7 +659,7 @@ public static IEnumerable<object[]> CreateTestInput(string instructionName)
token => ValidateSymbol(token, '-'),
token => ValidateKeyword(token, "chown"),
token => ValidateSymbol(token, '='),
token => ValidateAggregate<ChangeOwner>(token, "user:group",
token => ValidateAggregate<UserAccount>(token, "user:group",
token => ValidateLiteral(token, "user"),
token => ValidateSymbol(token, ':'),
token => ValidateLiteral(token, "group"))),
Expand Down Expand Up @@ -692,7 +692,7 @@ public class CreateTestScenario : TestScenario<TInstruction>
{
public string Destination { get; set; }
public IEnumerable<string> Sources { get; set; }
public ChangeOwner ChangeOwner { get; set; }
public UserAccount ChangeOwner { get; set; }
public string Permissions { get; set; }
public char EscapeChar { get; set; } = Dockerfile.DefaultEscapeChar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void BuildAllTokens()

Assert.Collection(builder.Tokens, new Action<Token>[]
{
token => ValidateAggregate<ChangeOwner>(token, "user",
token => ValidateAggregate<UserAccount>(token, "user",
token => ValidateLiteral(token, "user")),
token => ValidateAggregate<CommentToken>(token, "#comment",
token => ValidateSymbol(token, '#'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

namespace DockerfileModel.Tests
{
public class ChangeOwnerTests
public class UserAccountTests
{
[Theory]
[MemberData(nameof(ParseTestInput))]
public void Parse(ChangeOwnerParseTestScenario scenario)
public void Parse(UserAccountParseTestScenario scenario)
{
if (scenario.ParseExceptionPosition is null)
{
ChangeOwner result = ChangeOwner.Parse(scenario.Text, scenario.EscapeChar);
UserAccount result = UserAccount.Parse(scenario.Text, scenario.EscapeChar);
Assert.Equal(scenario.Text, result.ToString());
Assert.Collection(result.Tokens, scenario.TokenValidators);
scenario.Validate?.Invoke(result);
Expand All @@ -35,93 +35,93 @@ public void Parse(ChangeOwnerParseTestScenario scenario)
[MemberData(nameof(CreateTestInput))]
public void Create(CreateTestScenario scenario)
{
ChangeOwner result = new ChangeOwner(scenario.User, scenario.Group);
UserAccount result = new UserAccount(scenario.User, scenario.Group);
Assert.Collection(result.Tokens, scenario.TokenValidators);
scenario.Validate?.Invoke(result);
}

[Fact]
public void User()
{
ChangeOwner changeOwner = new ChangeOwner("test", "group");
Assert.Equal("test", changeOwner.User);
Assert.Equal("test", changeOwner.UserToken.Value);
UserAccount UserAccount = new UserAccount("test", "group");
Assert.Equal("test", UserAccount.User);
Assert.Equal("test", UserAccount.UserToken.Value);

changeOwner.User = "test2";
Assert.Equal("test2", changeOwner.User);
Assert.Equal("test2", changeOwner.UserToken.Value);
UserAccount.User = "test2";
Assert.Equal("test2", UserAccount.User);
Assert.Equal("test2", UserAccount.UserToken.Value);

changeOwner.UserToken.Value = "test3";
Assert.Equal("test3", changeOwner.User);
Assert.Equal("test3", changeOwner.UserToken.Value);
UserAccount.UserToken.Value = "test3";
Assert.Equal("test3", UserAccount.User);
Assert.Equal("test3", UserAccount.UserToken.Value);

changeOwner.UserToken = new LiteralToken("test4");
Assert.Equal("test4", changeOwner.User);
Assert.Equal("test4", changeOwner.UserToken.Value);
Assert.Equal("test4:group", changeOwner.ToString());
UserAccount.UserToken = new LiteralToken("test4");
Assert.Equal("test4", UserAccount.User);
Assert.Equal("test4", UserAccount.UserToken.Value);
Assert.Equal("test4:group", UserAccount.ToString());

Assert.Throws<ArgumentException>(() => changeOwner.User = "");
Assert.Throws<ArgumentNullException>(() => changeOwner.User = null);
Assert.Throws<ArgumentNullException>(() => changeOwner.UserToken = null);
Assert.Throws<ArgumentException>(() => UserAccount.User = "");
Assert.Throws<ArgumentNullException>(() => UserAccount.User = null);
Assert.Throws<ArgumentNullException>(() => UserAccount.UserToken = null);
}

[Fact]
public void Group()
{
ChangeOwner changeOwner = new ChangeOwner("user", "test");
Assert.Equal("test", changeOwner.Group);
Assert.Equal("test", changeOwner.GroupToken.Value);
UserAccount UserAccount = new UserAccount("user", "test");
Assert.Equal("test", UserAccount.Group);
Assert.Equal("test", UserAccount.GroupToken.Value);

changeOwner.Group = "test2";
Assert.Equal("test2", changeOwner.Group);
Assert.Equal("test2", changeOwner.GroupToken.Value);
UserAccount.Group = "test2";
Assert.Equal("test2", UserAccount.Group);
Assert.Equal("test2", UserAccount.GroupToken.Value);

changeOwner.GroupToken.Value = "test3";
Assert.Equal("test3", changeOwner.Group);
Assert.Equal("test3", changeOwner.GroupToken.Value);
UserAccount.GroupToken.Value = "test3";
Assert.Equal("test3", UserAccount.Group);
Assert.Equal("test3", UserAccount.GroupToken.Value);

changeOwner.Group = null;
Assert.Null(changeOwner.Group);
Assert.Null(changeOwner.GroupToken);
Assert.Equal("user", changeOwner.ToString());
UserAccount.Group = null;
Assert.Null(UserAccount.Group);
Assert.Null(UserAccount.GroupToken);
Assert.Equal("user", UserAccount.ToString());

changeOwner.GroupToken = new LiteralToken("test4");
Assert.Equal("test4", changeOwner.Group);
Assert.Equal("test4", changeOwner.GroupToken.Value);
Assert.Equal("user:test4", changeOwner.ToString());
UserAccount.GroupToken = new LiteralToken("test4");
Assert.Equal("test4", UserAccount.Group);
Assert.Equal("test4", UserAccount.GroupToken.Value);
Assert.Equal("user:test4", UserAccount.ToString());

changeOwner.GroupToken = null;
Assert.Null(changeOwner.Group);
Assert.Null(changeOwner.GroupToken);
Assert.Equal("user", changeOwner.ToString());
UserAccount.GroupToken = null;
Assert.Null(UserAccount.Group);
Assert.Null(UserAccount.GroupToken);
Assert.Equal("user", UserAccount.ToString());

changeOwner.Group = "";
Assert.Null(changeOwner.Group);
Assert.Null(changeOwner.GroupToken);
Assert.Equal("user", changeOwner.ToString());
UserAccount.Group = "";
Assert.Null(UserAccount.Group);
Assert.Null(UserAccount.GroupToken);
Assert.Equal("user", UserAccount.ToString());
}

[Fact]
public void UserWithVariables()
{
ChangeOwner changeOwner = new ChangeOwner("$var", "group");
UserAccount UserAccount = new UserAccount("$var", "group");
TestHelper.TestVariablesWithLiteral(
() => changeOwner.UserToken, "var", canContainVariables: true);
() => UserAccount.UserToken, "var", canContainVariables: true);
}

[Fact]
public void GroupWithVariables()
{
ChangeOwner changeOwner = new ChangeOwner("user", "$var");
UserAccount UserAccount = new UserAccount("user", "$var");
TestHelper.TestVariablesWithNullableLiteral(
() => changeOwner.GroupToken, token => changeOwner.GroupToken = token, val => changeOwner.Group = val, "var", canContainVariables: true);
() => UserAccount.GroupToken, token => UserAccount.GroupToken = token, val => UserAccount.Group = val, "var", canContainVariables: true);
}

public static IEnumerable<object[]> ParseTestInput()
{
ChangeOwnerParseTestScenario[] testInputs = new ChangeOwnerParseTestScenario[]
UserAccountParseTestScenario[] testInputs = new UserAccountParseTestScenario[]
{
new ChangeOwnerParseTestScenario
new UserAccountParseTestScenario
{
Text = "55:mygroup",
TokenValidators = new Action<Token>[]
Expand All @@ -136,7 +136,7 @@ public static IEnumerable<object[]> ParseTestInput()
Assert.Equal("mygroup", result.Group);
}
},
new ChangeOwnerParseTestScenario
new UserAccountParseTestScenario
{
Text = "bin",
TokenValidators = new Action<Token>[]
Expand All @@ -149,7 +149,7 @@ public static IEnumerable<object[]> ParseTestInput()
Assert.Null(result.Group);
}
},
new ChangeOwnerParseTestScenario
new UserAccountParseTestScenario
{
EscapeChar = '`',
Text = "us`\ner`\n:`\ngr`\noup",
Expand All @@ -176,7 +176,7 @@ public static IEnumerable<object[]> ParseTestInput()
Assert.Equal("us`\ner`\n", result.ToString());
}
},
new ChangeOwnerParseTestScenario
new UserAccountParseTestScenario
{
Text = "$user:group$var",
TokenValidators = new Action<Token>[]
Expand All @@ -191,12 +191,12 @@ public static IEnumerable<object[]> ParseTestInput()
token => ValidateString(token, "var")))
}
},
new ChangeOwnerParseTestScenario
new UserAccountParseTestScenario
{
Text = "user:",
ParseExceptionPosition = new Position(1, 1, 1)
},
new ChangeOwnerParseTestScenario
new UserAccountParseTestScenario
{
Text = ":group",
ParseExceptionPosition = new Position(1, 1, 1)
Expand Down Expand Up @@ -245,12 +245,12 @@ public static IEnumerable<object[]> CreateTestInput()
return testInputs.Select(input => new object[] { input });
}

public class ChangeOwnerParseTestScenario : ParseTestScenario<ChangeOwner>
public class UserAccountParseTestScenario : ParseTestScenario<UserAccount>
{
public char EscapeChar { get; set; }
}

public class CreateTestScenario : TestScenario<ChangeOwner>
public class CreateTestScenario : TestScenario<UserAccount>
{
public string User { get; set; }
public string Group { get; set; }
Expand Down
Loading

0 comments on commit fff59e1

Please sign in to comment.