Skip to content

Commit 21049e9

Browse files
committed
fix: additional tests
1 parent 88b98db commit 21049e9

File tree

6 files changed

+88
-74
lines changed

6 files changed

+88
-74
lines changed

internal/config/config_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,21 @@ func Test_TokenParser_config(t *testing.T) {
164164
})
165165
}
166166
}
167+
168+
func TestLookupIdent(t *testing.T) {
169+
ttests := map[string]struct {
170+
char string
171+
expect config.TokenType
172+
}{
173+
"new line": {"\n", config.NEW_LINE},
174+
"dash": {"-", config.TEXT},
175+
}
176+
for name, tt := range ttests {
177+
t.Run(name, func(t *testing.T) {
178+
got := config.LookupIdent(tt.char)
179+
if got != tt.expect {
180+
t.Errorf("got %v wanted %v", got, tt.expect)
181+
}
182+
})
183+
}
184+
}

internal/config/token.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,3 @@ func LookupIdent(ident string) TokenType {
7878
}
7979
return TEXT
8080
}
81-
82-
// var typeMapper = map[string]TokenType{
83-
// "MESSAGE": MESSAGE,
84-
// "OPERATION": OPERATION,
85-
// "CHANNEL": CHANNEL,
86-
// "INFO": INFO,
87-
// "SERVER": SERVER,
88-
// }
89-
90-
// func LookupType(typ string) TokenType {
91-
// if tok, ok := typeMapper[strings.ToUpper(typ)]; ok {
92-
// return tok
93-
// }
94-
// return ""
95-
// }

internal/lexer/lexer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ func (l *Lexer) NextToken() config.Token {
156156
case 0:
157157
tok.Literal = ""
158158
tok.Type = config.EOF
159-
// case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
160-
// 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z',
161-
// 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z':
162159
default:
163160
if isText(l.ch) {
164161
tok.Literal = l.readText()

internal/parser/parser.go

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ func New(l *lexer.Lexer, c *config.GenVarsConfig) *Parser {
5555
return p
5656
}
5757

58-
func (p *Parser) WithEnvironment(environ []string) *Parser {
59-
p.environ = environ
60-
return p
61-
}
62-
6358
func (p *Parser) WithLogger(logger log.ILogger) *Parser {
6459
p.log = nil //speed up GC
6560
p.log = logger
@@ -126,9 +121,6 @@ func (p *Parser) buildConfigManagerTokenFromBlocks(configManagerToken *config.Pa
126121
// built as part of the below parser
127122
sanitizedToken := ""
128123

129-
// should exit the loop if no end tag found
130-
notFoundEnd := true
131-
132124
// stop on end of file
133125
for !p.peekTokenIs(config.EOF) {
134126
// // This is the target state when there is an optional token wrapping
@@ -145,7 +137,6 @@ func (p *Parser) buildConfigManagerTokenFromBlocks(configManagerToken *config.Pa
145137
// when next token is another token
146138
// i.e. the tokens are adjacent
147139
if p.peekTokenIs(config.BEGIN_CONFIGMANAGER_TOKEN) {
148-
notFoundEnd = false
149140
fullToken += p.curToken.Literal
150141
sanitizedToken += p.curToken.Literal
151142
stmt.EndToken = p.curToken
@@ -156,7 +147,6 @@ func (p *Parser) buildConfigManagerTokenFromBlocks(configManagerToken *config.Pa
156147
if p.peekTokenIsEnd() {
157148
// we want set the current token as both the full and sanitized
158149
// the current lexer token is the entire configmanager token
159-
notFoundEnd = false
160150
fullToken += p.curToken.Literal
161151
sanitizedToken += p.curToken.Literal
162152
stmt.EndToken = p.curToken
@@ -175,7 +165,6 @@ func (p *Parser) buildConfigManagerTokenFromBlocks(configManagerToken *config.Pa
175165
p.errors = append(p.errors, wrapErr(fullToken, currentToken.Line, currentToken.Column, err))
176166
return nil
177167
}
178-
notFoundEnd = false
179168
// keyPath would have built the keyPath and metadata if any
180169
break
181170
}
@@ -187,7 +176,6 @@ func (p *Parser) buildConfigManagerTokenFromBlocks(configManagerToken *config.Pa
187176
p.errors = append(p.errors, wrapErr(fullToken, currentToken.Line, currentToken.Column, err))
188177
return nil
189178
}
190-
notFoundEnd = false
191179
break
192180
}
193181

@@ -199,18 +187,17 @@ func (p *Parser) buildConfigManagerTokenFromBlocks(configManagerToken *config.Pa
199187
// else it would be lost once the parser is advanced below
200188
p.nextToken()
201189
if p.peekTokenIs(config.EOF) {
202-
notFoundEnd = false
203190
fullToken += p.curToken.Literal
204191
sanitizedToken += p.curToken.Literal
205192
stmt.EndToken = p.curToken
206193
break
207194
}
208195
}
209196

210-
if notFoundEnd {
211-
p.errors = append(p.errors, wrapErr(fullToken, currentToken.Line, currentToken.Column, ErrNoEndTagFound))
212-
return nil
213-
}
197+
// if notFoundEnd {
198+
// p.errors = append(p.errors, wrapErr(fullToken, currentToken.Line, currentToken.Column, ErrNoEndTagFound))
199+
// return nil
200+
// }
214201

215202
configManagerToken.WithSanitizedToken(sanitizedToken)
216203
stmt.ParsedToken = *configManagerToken
@@ -269,13 +256,8 @@ func (p *Parser) buildMetadata(configManagerToken *config.ParsedTokenConfig) err
269256
p.nextToken()
270257
for !p.peekTokenIs(config.EOF) {
271258
if p.peekTokenIsEnd() {
272-
if p.currentTokenIs(config.END_META_CONFIGMANAGER_TOKEN) {
273-
metadata += p.curToken.Literal
274-
found = true
275-
p.nextToken()
276-
break
277-
}
278-
return fmt.Errorf("%w, metadata string has no closing", ErrNoEndTagFound)
259+
// next token is an end of token but no closing `]` found
260+
return fmt.Errorf("%w, metadata (%s) string has no closing", ErrNoEndTagFound, metadata)
279261
}
280262
if p.peekTokenIs(config.END_META_CONFIGMANAGER_TOKEN) {
281263
metadata += p.curToken.Literal
@@ -289,6 +271,7 @@ func (p *Parser) buildMetadata(configManagerToken *config.ParsedTokenConfig) err
289271
configManagerToken.WithMetadata(metadata)
290272

291273
if !found {
274+
// hit the end of file and no end tag found
292275
return fmt.Errorf("%w, metadata string has no closing", ErrNoEndTagFound)
293276
}
294277
return nil

internal/parser/parser_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Test_ParserBlocks(t *testing.T) {
2020
// prefix,path,keyLookup
2121
expected [][3]string
2222
}{
23-
"tokens touching each other in source": {
23+
"tokens touching each other in source after key path": {
2424
`foo stuyfsdfsf
2525
foo=AWSPARAMSTR:///path|keyAWSSECRETS:///foo
2626
other text her
@@ -42,6 +42,26 @@ func Test_ParserBlocks(t *testing.T) {
4242
{string(config.ParamStorePrefix), "/config", "qp2"},
4343
},
4444
},
45+
"tokens touching each other in source after metadata": {
46+
`foo stuyfsdfsf
47+
foo=AWSPARAMSTR:///path|key[meta=val]AWSSECRETS:///foo
48+
other text her
49+
BAR=something
50+
`, [][3]string{
51+
{string(config.ParamStorePrefix), "/path", "key"},
52+
{string(config.SecretMgrPrefix), "/foo", ""},
53+
},
54+
},
55+
"tokens touching each other in source": {
56+
`foo stuyfsdfsf
57+
foo=AWSPARAMSTR:///pathAWSSECRETS:///foo
58+
other text her
59+
BAR=something
60+
`, [][3]string{
61+
{string(config.ParamStorePrefix), "/path", ""},
62+
{string(config.SecretMgrPrefix), "/foo", ""},
63+
},
64+
},
4565
"touching EOF single token": {
4666
`AWSPARAMSTR:///config|qp2`,
4767
[][3]string{
@@ -99,6 +119,12 @@ func Test_Parse_should_fail_on_metadata(t *testing.T) {
99119
`AWSSECRETS:///foo|path.one[version=1.2.3`,
100120
parser.ErrNoEndTagFound,
101121
},
122+
"when _end_tag_found with keysPath in the middle": {
123+
`AWSSECRETS:///foo|path.one[version=1.2.3
124+
more content here
125+
`,
126+
parser.ErrNoEndTagFound,
127+
},
102128
"when no metadata has been supplied": {
103129
`AWSSECRETS:///foo|path.one[]`,
104130
parser.ErrMetadataEmpty,
@@ -134,6 +160,11 @@ func Test_Parse_should_pass_with_metadata_end_tag(t *testing.T) {
134160
`AWSSECRETS:///foo[version=1.2.3]`,
135161
`version=1.2.3`,
136162
},
163+
"without keysPath in the middle of content": {
164+
`AWSSECRETS:///foo[version=1.2.3]
165+
`,
166+
`version=1.2.3`,
167+
},
137168
"with keysPath": {
138169
`AWSSECRETS:///foo|path.one[version=1.2.3]`,
139170
`version=1.2.3`,

internal/strategy/strategy.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,6 @@ type StrategyFunc func(ctx context.Context, token *config.ParsedTokenConfig) (st
2020
// StrategyFuncMap
2121
type StrategyFuncMap map[config.ImplementationPrefix]StrategyFunc
2222

23-
func defaultStrategyFuncMap(logger log.ILogger) map[config.ImplementationPrefix]StrategyFunc {
24-
return map[config.ImplementationPrefix]StrategyFunc{
25-
config.AzTableStorePrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
26-
return store.NewAzTableStore(ctx, token, logger)
27-
},
28-
config.AzAppConfigPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
29-
return store.NewAzAppConf(ctx, token, logger)
30-
},
31-
config.GcpSecretsPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
32-
return store.NewGcpSecrets(ctx, logger)
33-
},
34-
config.SecretMgrPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
35-
return store.NewSecretsMgr(ctx, logger)
36-
},
37-
config.ParamStorePrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
38-
return store.NewParamStore(ctx, logger)
39-
},
40-
config.AzKeyVaultSecretsPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
41-
return store.NewKvScrtStore(ctx, token, logger)
42-
},
43-
config.HashicorpVaultPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
44-
return store.NewVaultStore(ctx, token, logger)
45-
},
46-
}
47-
}
48-
49-
type strategyFnMap struct {
50-
mu sync.Mutex
51-
funcMap StrategyFuncMap
52-
}
53-
5423
type Strategy struct {
5524
config config.GenVarsConfig
5625
strategyFuncMap strategyFnMap
@@ -122,3 +91,34 @@ func (tr *TokenResponse) Key() *config.ParsedTokenConfig {
12291
func (tr *TokenResponse) Value() string {
12392
return tr.value
12493
}
94+
95+
func defaultStrategyFuncMap(logger log.ILogger) StrategyFuncMap {
96+
return map[config.ImplementationPrefix]StrategyFunc{
97+
config.AzTableStorePrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
98+
return store.NewAzTableStore(ctx, token, logger)
99+
},
100+
config.AzAppConfigPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
101+
return store.NewAzAppConf(ctx, token, logger)
102+
},
103+
config.GcpSecretsPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
104+
return store.NewGcpSecrets(ctx, logger)
105+
},
106+
config.SecretMgrPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
107+
return store.NewSecretsMgr(ctx, logger)
108+
},
109+
config.ParamStorePrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
110+
return store.NewParamStore(ctx, logger)
111+
},
112+
config.AzKeyVaultSecretsPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
113+
return store.NewKvScrtStore(ctx, token, logger)
114+
},
115+
config.HashicorpVaultPrefix: func(ctx context.Context, token *config.ParsedTokenConfig) (store.Strategy, error) {
116+
return store.NewVaultStore(ctx, token, logger)
117+
},
118+
}
119+
}
120+
121+
type strategyFnMap struct {
122+
mu sync.Mutex
123+
funcMap StrategyFuncMap
124+
}

0 commit comments

Comments
 (0)