Skip to content

Commit 1231110

Browse files
committed
error when there is more than one module:init or module:shutdown
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent c57e1c6 commit 1231110

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

internal/extgen/moduleParser.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ func (mp *ModuleParser) parse(filename string) (module *phpModule, err error) {
6969

7070
switch currentDirective {
7171
case "init":
72+
if module.InitFunc != "" {
73+
return nil, fmt.Errorf("duplicate init directive found at line %d: init function '%s' already defined", lineNumber, module.InitFunc)
74+
}
7275
module.InitFunc = funcName
7376
module.InitCode = funcCode
7477
case "shutdown":
78+
if module.ShutdownFunc != "" {
79+
return nil, fmt.Errorf("duplicate shutdown directive found at line %d: shutdown function '%s' already defined", lineNumber, module.ShutdownFunc)
80+
}
7581
module.ShutdownFunc = funcName
7682
module.ShutdownCode = funcCode
7783
}

internal/extgen/moduleParser_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,85 @@ func TestExtractGoFunction(t *testing.T) {
232232
}
233233
}
234234

235+
func TestModuleParserErrors(t *testing.T) {
236+
tests := []struct {
237+
name string
238+
input string
239+
expectedErr string
240+
}{
241+
{
242+
name: "duplicate init directives",
243+
input: `package main
244+
245+
//export_php:module init
246+
func firstInit() {
247+
// First init function
248+
}
249+
250+
//export_php:module init
251+
func secondInit() {
252+
// Second init function - should error
253+
}`,
254+
expectedErr: "duplicate init directive",
255+
},
256+
{
257+
name: "duplicate shutdown directives",
258+
input: `package main
259+
260+
//export_php:module shutdown
261+
func firstShutdown() {
262+
// First shutdown function
263+
}
264+
265+
//export_php:module shutdown
266+
func secondShutdown() {
267+
// Second shutdown function - should error
268+
}`,
269+
expectedErr: "duplicate shutdown directive",
270+
},
271+
{
272+
name: "multiple duplicates",
273+
input: `package main
274+
275+
//export_php:module init
276+
func firstInit() {
277+
// First init function
278+
}
279+
280+
//export_php:module init
281+
func secondInit() {
282+
// Duplicate init - should error
283+
}
284+
285+
//export_php:module shutdown
286+
func firstShutdown() {
287+
// First shutdown function
288+
}
289+
290+
//export_php:module shutdown
291+
func secondShutdown() {
292+
// Duplicate shutdown - should error
293+
}`,
294+
expectedErr: "duplicate init directive",
295+
},
296+
}
297+
298+
for _, tt := range tests {
299+
t.Run(tt.name, func(t *testing.T) {
300+
tmpDir := t.TempDir()
301+
fileName := filepath.Join(tmpDir, tt.name+".go")
302+
require.NoError(t, os.WriteFile(fileName, []byte(tt.input), 0644))
303+
304+
parser := &ModuleParser{}
305+
module, err := parser.parse(fileName)
306+
307+
assert.Error(t, err, "parse() should return error for duplicate directives")
308+
assert.Contains(t, err.Error(), tt.expectedErr, "error message should contain expected text")
309+
assert.Nil(t, module, "parse() should return nil when there's an error")
310+
})
311+
}
312+
}
313+
235314
func TestModuleParserFileErrors(t *testing.T) {
236315
parser := &ModuleParser{}
237316

0 commit comments

Comments
 (0)