Skip to content

Commit a0e2a1c

Browse files
committed
Guard all type assertions in visitor tests with ok check
Replace 13 unguarded prog.Statements[0].(*ast.XxxStmt) direct assertions with guarded two-value form + t.Fatalf across visitor_settings_test.go, visitor_security_test.go, and visitor_lint_test.go to prevent panics on unexpected types.
1 parent 94891e8 commit a0e2a1c

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

mdl/visitor/visitor_lint_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func TestLint_ModuleOnly(t *testing.T) {
3838
}
3939
return
4040
}
41-
stmt := prog.Statements[0].(*ast.LintStmt)
41+
stmt, ok := prog.Statements[0].(*ast.LintStmt)
42+
if !ok {
43+
t.Fatalf("Expected LintStmt, got %T", prog.Statements[0])
44+
}
4245
if !stmt.ModuleOnly {
4346
t.Error("Expected ModuleOnly true")
4447
}
@@ -62,7 +65,10 @@ func TestLint_WithFormat(t *testing.T) {
6265
}
6366
return
6467
}
65-
stmt := prog.Statements[0].(*ast.LintStmt)
68+
stmt, ok := prog.Statements[0].(*ast.LintStmt)
69+
if !ok {
70+
t.Fatalf("Expected LintStmt, got %T", prog.Statements[0])
71+
}
6672
if stmt.Format != "json" {
6773
t.Errorf("Expected json format, got %q", stmt.Format)
6874
}
@@ -77,7 +83,10 @@ func TestShowLintRules(t *testing.T) {
7783
}
7884
return
7985
}
80-
stmt := prog.Statements[0].(*ast.LintStmt)
86+
stmt, ok := prog.Statements[0].(*ast.LintStmt)
87+
if !ok {
88+
t.Fatalf("Expected LintStmt, got %T", prog.Statements[0])
89+
}
8190
if !stmt.ShowRules {
8291
t.Error("Expected ShowRules true")
8392
}

mdl/visitor/visitor_security_test.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ func TestCreateModuleRole_NoDescription(t *testing.T) {
4141
}
4242
return
4343
}
44-
stmt := prog.Statements[0].(*ast.CreateModuleRoleStmt)
44+
stmt, ok := prog.Statements[0].(*ast.CreateModuleRoleStmt)
45+
if !ok {
46+
t.Fatalf("Expected CreateModuleRoleStmt, got %T", prog.Statements[0])
47+
}
4548
if stmt.Description != "" {
4649
t.Errorf("Expected empty description, got %q", stmt.Description)
4750
}
@@ -104,7 +107,10 @@ func TestCreateUserRole_OrModify(t *testing.T) {
104107
}
105108
return
106109
}
107-
stmt := prog.Statements[0].(*ast.CreateUserRoleStmt)
110+
stmt, ok := prog.Statements[0].(*ast.CreateUserRoleStmt)
111+
if !ok {
112+
t.Fatalf("Expected CreateUserRoleStmt, got %T", prog.Statements[0])
113+
}
108114
if !stmt.CreateOrModify {
109115
t.Error("Expected CreateOrModify true")
110116
}
@@ -146,7 +152,10 @@ func TestAlterUserRole_Remove(t *testing.T) {
146152
}
147153
return
148154
}
149-
stmt := prog.Statements[0].(*ast.AlterUserRoleStmt)
155+
stmt, ok := prog.Statements[0].(*ast.AlterUserRoleStmt)
156+
if !ok {
157+
t.Fatalf("Expected AlterUserRoleStmt, got %T", prog.Statements[0])
158+
}
150159
if stmt.Add {
151160
t.Error("Expected Add false")
152161
}
@@ -229,7 +238,10 @@ func TestGrantEntityAccess_MultipleRoles(t *testing.T) {
229238
}
230239
return
231240
}
232-
stmt := prog.Statements[0].(*ast.GrantEntityAccessStmt)
241+
stmt, ok := prog.Statements[0].(*ast.GrantEntityAccessStmt)
242+
if !ok {
243+
t.Fatalf("Expected GrantEntityAccessStmt, got %T", prog.Statements[0])
244+
}
233245
if len(stmt.Roles) != 2 {
234246
t.Fatalf("Expected 2 roles, got %d", len(stmt.Roles))
235247
}
@@ -265,7 +277,10 @@ func TestRevokeEntityAccess_Partial(t *testing.T) {
265277
}
266278
return
267279
}
268-
stmt := prog.Statements[0].(*ast.RevokeEntityAccessStmt)
280+
stmt, ok := prog.Statements[0].(*ast.RevokeEntityAccessStmt)
281+
if !ok {
282+
t.Fatalf("Expected RevokeEntityAccessStmt, got %T", prog.Statements[0])
283+
}
269284
if len(stmt.Rights) != 1 {
270285
t.Fatalf("Expected 1 right, got %d", len(stmt.Rights))
271286
}
@@ -500,7 +515,10 @@ func TestAlterProjectSecurity_DemoUsers(t *testing.T) {
500515
}
501516
return
502517
}
503-
stmt := prog.Statements[0].(*ast.AlterProjectSecurityStmt)
518+
stmt, ok := prog.Statements[0].(*ast.AlterProjectSecurityStmt)
519+
if !ok {
520+
t.Fatalf("Expected AlterProjectSecurityStmt, got %T", prog.Statements[0])
521+
}
504522
if stmt.SecurityLevel != "" {
505523
t.Errorf("Expected empty security level, got %q", stmt.SecurityLevel)
506524
}
@@ -545,7 +563,10 @@ func TestCreateDemoUser_OrModify(t *testing.T) {
545563
}
546564
return
547565
}
548-
stmt := prog.Statements[0].(*ast.CreateDemoUserStmt)
566+
stmt, ok := prog.Statements[0].(*ast.CreateDemoUserStmt)
567+
if !ok {
568+
t.Fatalf("Expected CreateDemoUserStmt, got %T", prog.Statements[0])
569+
}
549570
if !stmt.CreateOrModify {
550571
t.Error("Expected CreateOrModify true")
551572
}
@@ -596,7 +617,10 @@ func TestUpdateSecurity_InModule(t *testing.T) {
596617
}
597618
return
598619
}
599-
stmt := prog.Statements[0].(*ast.UpdateSecurityStmt)
620+
stmt, ok := prog.Statements[0].(*ast.UpdateSecurityStmt)
621+
if !ok {
622+
t.Fatalf("Expected UpdateSecurityStmt, got %T", prog.Statements[0])
623+
}
600624
if stmt.Module != "MyModule" {
601625
t.Errorf("Expected MyModule, got %q", stmt.Module)
602626
}

mdl/visitor/visitor_settings_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func TestAlterSettings_Constant(t *testing.T) {
3838
}
3939
return
4040
}
41-
stmt := prog.Statements[0].(*ast.AlterSettingsStmt)
41+
stmt, ok := prog.Statements[0].(*ast.AlterSettingsStmt)
42+
if !ok {
43+
t.Fatalf("Expected AlterSettingsStmt, got %T", prog.Statements[0])
44+
}
4245
if stmt.ConstantId != "MyModule.APIEndpoint" {
4346
t.Errorf("Got ConstantId %q", stmt.ConstantId)
4447
}
@@ -56,7 +59,10 @@ func TestAlterSettings_DropConstant(t *testing.T) {
5659
}
5760
return
5861
}
59-
stmt := prog.Statements[0].(*ast.AlterSettingsStmt)
62+
stmt, ok := prog.Statements[0].(*ast.AlterSettingsStmt)
63+
if !ok {
64+
t.Fatalf("Expected AlterSettingsStmt, got %T", prog.Statements[0])
65+
}
6066
if !stmt.DropConstant {
6167
t.Error("Expected DropConstant true")
6268
}

0 commit comments

Comments
 (0)