-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from vladimirvivien/parser-package-reorg
Decouple script parsing from script representation by introducing a new `parser` package which is responsible for parsing and creating a `script.Script`
- Loading branch information
Showing
34 changed files
with
3,370 additions
and
1,752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
.vscode | ||
.vendor | ||
.build | ||
dist | ||
dist | ||
.idea | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright (c) 2019 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/vmware-tanzu/crash-diagnostics/script" | ||
) | ||
|
||
func TestCommandAS(t *testing.T) { | ||
tests := []parserTest{ | ||
{ | ||
name: "AS", | ||
source: func(t *testing.T) string { | ||
return "AS userid:foo groupid:bar" | ||
}, | ||
script: func(t *testing.T, s *script.Script) { | ||
cmds := s.Preambles[script.CmdAs] | ||
if len(cmds) != 1 { | ||
t.Errorf("Script missing preamble %s", script.CmdAs) | ||
} | ||
asCmd, ok := cmds[0].(*script.AsCommand) | ||
if !ok { | ||
t.Errorf("Unexpected type %T in script", cmds[0]) | ||
} | ||
if asCmd.GetUserId() != "foo" { | ||
t.Errorf("Unexpected AS userid %s", asCmd.GetUserId()) | ||
} | ||
if asCmd.GetGroupId() != "bar" { | ||
t.Errorf("Unexpected AS groupid %s", asCmd.GetUserId()) | ||
} | ||
}, | ||
}, | ||
// { | ||
// name: "AS with quoted userid and groupid", | ||
// source: func() string { | ||
// return `AS userid:"foo" groupid:bar` | ||
// }, | ||
// script: func(s *Script) error { | ||
// cmds := s.Preambles[CmdAs] | ||
// if len(cmds) != 1 { | ||
// return fmt.Errorf("Script missing preamble %s", CmdAs) | ||
// } | ||
// asCmd, ok := cmds[0].(*AsCommand) | ||
// if !ok { | ||
// return fmt.Errorf("Unexpected type %T in script", cmds[0]) | ||
// } | ||
// if asCmd.GetUserId() != "foo" { | ||
// return fmt.Errorf("Unexpected AS userid %s", asCmd.GetUserId()) | ||
// } | ||
// if asCmd.GetGroupId() != "bar" { | ||
// return fmt.Errorf("Unexpected AS groupid %s", asCmd.GetUserId()) | ||
// } | ||
// return nil | ||
// }, | ||
// }, | ||
// { | ||
// name: "AS with only userid", | ||
// source: func() string { | ||
// return "AS userid:foo" | ||
// }, | ||
// script: func(s *Script) error { | ||
// cmds := s.Preambles[CmdAs] | ||
// if len(cmds) != 1 { | ||
// return fmt.Errorf("Script missing preamble %s", CmdAs) | ||
// } | ||
// asCmd, ok := cmds[0].(*AsCommand) | ||
// if !ok { | ||
// return fmt.Errorf("Unexpected type %T in script", cmds[0]) | ||
// } | ||
// if asCmd.GetUserId() != "foo" { | ||
// return fmt.Errorf("Unexpected AS userid %s", asCmd.GetUserId()) | ||
// } | ||
// if asCmd.GetGroupId() != fmt.Sprintf("%d", os.Getgid()) { | ||
// return fmt.Errorf("Unexpected AS groupid %s", asCmd.GetGroupId()) | ||
// } | ||
// return nil | ||
// }, | ||
// }, | ||
// { | ||
// name: "AS not specified", | ||
// source: func() string { | ||
// return "FROM local" | ||
// }, | ||
// script: func(s *Script) error { | ||
// cmds := s.Preambles[CmdAs] | ||
// if len(cmds) != 1 { | ||
// return fmt.Errorf("Script missing default AS preamble") | ||
// } | ||
// asCmd, ok := cmds[0].(*AsCommand) | ||
// if !ok { | ||
// return fmt.Errorf("Unexpected type %T in script", cmds[0]) | ||
// } | ||
// if asCmd.GetUserId() != fmt.Sprintf("%d", os.Getuid()) { | ||
// return fmt.Errorf("Unexpected AS default userid %s", asCmd.GetUserId()) | ||
// } | ||
// if asCmd.GetGroupId() != fmt.Sprintf("%d", os.Getgid()) { | ||
// return fmt.Errorf("Unexpected AS default groupid %s", asCmd.GetUserId()) | ||
// } | ||
// return nil | ||
// }, | ||
// }, | ||
// { | ||
// name: "Multiple AS provided", | ||
// source: func() string { | ||
// return "AS userid:foo\nAS userid:bar" | ||
// }, | ||
// script: func(s *Script) error { | ||
// cmds := s.Preambles[CmdAs] | ||
// if len(cmds) != 1 { | ||
// return fmt.Errorf("Script should only have 1 AS instruction, got %d", len(cmds)) | ||
// } | ||
// asCmd := cmds[0].(*AsCommand) | ||
// if asCmd.GetUserId() != "bar" { | ||
// return fmt.Errorf("Unexpected AS userid %s", asCmd.GetUserId()) | ||
// } | ||
// if asCmd.GetGroupId() != "" { | ||
// return fmt.Errorf("Unexpected AS groupid %s", asCmd.GetUserId()) | ||
// } | ||
// return nil | ||
// }, | ||
// shouldFail: true, | ||
// }, | ||
// { | ||
// name: "AS with var expansion", | ||
// source: func() string { | ||
// os.Setenv("foogid", "barid") | ||
// return "AS userid:$USER groupid:$foogid" | ||
// }, | ||
// script: func(s *Script) error { | ||
// cmds := s.Preambles[CmdAs] | ||
// asCmd := cmds[0].(*AsCommand) | ||
// if asCmd.GetUserId() != ExpandEnv("$USER") { | ||
// return fmt.Errorf("Unexpected AS userid %s", asCmd.GetUserId()) | ||
// } | ||
// if asCmd.GetGroupId() != "barid" { | ||
// return fmt.Errorf("Unexpected AS groupid %s", asCmd.GetUserId()) | ||
// } | ||
// return nil | ||
// }, | ||
// }, | ||
// { | ||
// name: "AS with multiple args", | ||
// source: func() string { | ||
// return "AS foo:bar fuzz:buzz" | ||
// }, | ||
// shouldFail: true, | ||
// }, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
runParserTest(t, test) | ||
}) | ||
} | ||
} |
Oops, something went wrong.