Skip to content

Commit e7ef431

Browse files
antonsyndclaude
andcommitted
refactor: Remove WrapWithMainIfNeeded helper and fix remaining tests
Complete Phase 1 infrastructure removal: - Remove WrapWithMainIfNeeded call from IntegrationTestBase.cs - Remove WrapWithMainIfNeeded call from ProjectCompilationHelper.cs - Delete WrapWithMainIfNeeded method from TestHelpers.cs (118 lines) Fix remaining tests that weren't updated in previous commits: - DivisionDeviationTests.cs: Add main() to all 10 tests - Phase0110IntegrationTests.cs: Add main() to 27 entry point files - Phase012IntegrationTests.cs: Update edge case tests to expect failure for empty/whitespace files without main() - CrossModuleInheritanceTests.cs: Add main() to .NET inheritance test All 4032 tests pass. grep -r "WrapWithMainIfNeeded" src/ returns empty. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9452656 commit e7ef431

File tree

8 files changed

+174
-241
lines changed

8 files changed

+174
-241
lines changed

src/Sharpy.Compiler.Tests/Helpers/ProjectCompilationHelper.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,6 @@ public ProjectCompilationHelper AddSourceFile(string relativePath, string conten
119119
Directory.CreateDirectory(directory);
120120
}
121121

122-
// Wrap entry point files in main() if needed
123-
var isEntry = isEntryPoint ?? (Options.EntryPoint == relativePath || relativePath == "main.spy");
124-
if (isEntry)
125-
{
126-
content = TestHelpers.WrapWithMainIfNeeded(content);
127-
}
128-
129122
File.WriteAllText(fullPath, content);
130123
_sourceFiles.Add(fullPath);
131124

src/Sharpy.Compiler.Tests/Integration/CrossModuleInheritanceTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ class CustomError(Exception):
303303
def __init__(self, message: str, code: int):
304304
super().__init__(message)
305305
self.code = code
306+
307+
def main():
308+
err = CustomError('test error', 42)
306309
";
307310

308311
var result = CompileAndExecute(source);

src/Sharpy.Compiler.Tests/Integration/IntegrationTestBase.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ protected class ExecutionResult
4949
/// <param name="executionTimeoutMs">Optional timeout in milliseconds for execution. Default is no timeout (0). Use for tests that may have infinite loops.</param>
5050
protected ExecutionResult CompileAndExecute(string sharpySource, string fileName = "test.spy", int executionTimeoutMs = 0)
5151
{
52-
// Wrap source in main() if needed for entry point compliance
53-
sharpySource = TestHelpers.WrapWithMainIfNeeded(sharpySource);
54-
5552
// Track path to Sharpy.Core for copying to temp execution directory
5653
string? runtimePath = null;
5754

src/Sharpy.Compiler.Tests/Integration/Phase0110IntegrationTests.cs

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def helper() -> str:
5050
helper.AddSourceFile("main.spy", @"
5151
import utils
5252
53-
result = utils.helper()
53+
def main():
54+
result = utils.helper()
5455
");
5556

5657
helper.WithEntryPoint("main.spy");
@@ -72,7 +73,8 @@ def add(x: int, y: int) -> int:
7273
helper.AddSourceFile("main.spy", @"
7374
import lib.math
7475
75-
result = lib.math.add(5, 3)
76+
def main():
77+
result = lib.math.add(5, 3)
7678
");
7779

7880
helper.WithEntryPoint("main.spy");
@@ -100,8 +102,9 @@ def concat(a: str, b: str) -> str:
100102
import math_ops
101103
import string_ops
102104
103-
num = math_ops.add(5, 3)
104-
text = string_ops.concat('hello', 'world')
105+
def main():
106+
num = math_ops.add(5, 3)
107+
text = string_ops.concat('hello', 'world')
105108
");
106109

107110
helper.WithEntryPoint("main.spy");
@@ -123,8 +126,9 @@ public void BasicImport_ImportVariable_Works()
123126
helper.AddSourceFile("main.spy", @"
124127
import config
125128
126-
size = config.MAX_SIZE
127-
name = config.APP_NAME
129+
def main():
130+
size = config.MAX_SIZE
131+
name = config.APP_NAME
128132
");
129133

130134
helper.WithEntryPoint("main.spy");
@@ -154,6 +158,9 @@ def func_b() -> int:
154158

155159
helper.AddSourceFile("main.spy", @"
156160
import module_a
161+
162+
def main():
163+
pass
157164
");
158165

159166
helper.WithEntryPoint("main.spy");
@@ -172,7 +179,8 @@ public void BasicImport_ModuleNotFound_ReportsError()
172179
helper.AddSourceFile("main.spy", @"
173180
import nonexistent_module
174181
175-
x = nonexistent_module.func()
182+
def main():
183+
x = nonexistent_module.func()
176184
");
177185

178186
helper.WithEntryPoint("main.spy");
@@ -200,7 +208,8 @@ def helper() -> str:
200208
helper.AddSourceFile("main.spy", @"
201209
import mypackage.module
202210
203-
result = mypackage.module.helper()
211+
def main():
212+
result = mypackage.module.helper()
204213
");
205214

206215
helper.WithEntryPoint("main.spy");
@@ -232,8 +241,9 @@ def utility_func() -> str:
232241
helper.AddSourceFile("main.spy", @"
233242
import mypackage
234243
235-
result = mypackage.utility_func()
236-
value = mypackage.DATA_VALUE
244+
def main():
245+
result = mypackage.utility_func()
246+
value = mypackage.DATA_VALUE
237247
");
238248

239249
helper.WithEntryPoint("main.spy");
@@ -255,8 +265,9 @@ public void PackageInit_WithVariables_DefinesPackageLevelVariables()
255265
helper.AddSourceFile("main.spy", @"
256266
import mypackage
257267
258-
version = mypackage.VERSION
259-
debug = mypackage.DEBUG
268+
def main():
269+
version = mypackage.VERSION
270+
debug = mypackage.DEBUG
260271
");
261272

262273
helper.WithEntryPoint("main.spy");
@@ -281,8 +292,9 @@ def helper() -> int:
281292
helper.AddSourceFile("main.spy", @"
282293
import mypackage
283294
284-
result = mypackage.package_function()
285-
value = mypackage.helper()
295+
def main():
296+
result = mypackage.package_function()
297+
value = mypackage.helper()
286298
");
287299

288300
helper.WithEntryPoint("main.spy");
@@ -305,7 +317,8 @@ def child_func() -> str:
305317
helper.AddSourceFile("main.spy", @"
306318
import parent.child
307319
308-
result = parent.child.child_func()
320+
def main():
321+
result = parent.child.child_func()
309322
");
310323

311324
helper.WithEntryPoint("main.spy");
@@ -327,7 +340,8 @@ def format_string(s: str) -> str:
327340
helper.AddSourceFile("main.spy", @"
328341
from utils import format_string
329342
330-
result = format_string('test')
343+
def main():
344+
result = format_string('test')
331345
");
332346

333347
helper.WithEntryPoint("main.spy");
@@ -350,7 +364,8 @@ public void ProjectFile_BasicConfiguration_CompilesSuccessfully()
350364
.WithEntryPoint("main.spy");
351365

352366
helper.AddSourceFile("main.spy", @"
353-
x: int = 42
367+
def main():
368+
x: int = 42
354369
");
355370

356371
helper.CreateProjectFile();
@@ -399,8 +414,9 @@ def helper() -> str:
399414
import utils
400415
import config
401416
402-
result = utils.helper()
403-
version = config.VERSION
417+
def main():
418+
result = utils.helper()
419+
version = config.VERSION
404420
");
405421

406422
helper.CreateProjectFile();
@@ -418,7 +434,8 @@ public void ProjectFile_CustomSourceDirectory_FindsSourceFiles()
418434
.WithEntryPoint("app.spy");
419435

420436
helper.AddSourceFile("app.spy", @"
421-
x: int = 100
437+
def main():
438+
x: int = 100
422439
");
423440

424441
helper.CreateProjectFile();
@@ -444,7 +461,8 @@ def compute() -> int:
444461
helper.AddSourceFile("main.spy", @"
445462
import dependency
446463
447-
result = dependency.compute()
464+
def main():
465+
result = dependency.compute()
448466
");
449467

450468
helper.WithEntryPoint("main.spy");
@@ -473,7 +491,8 @@ def middle_func() -> int:
473491
helper.AddSourceFile("main.spy", @"
474492
import middle
475493
476-
result = middle.middle_func()
494+
def main():
495+
result = middle.middle_func()
477496
");
478497

479498
helper.WithEntryPoint("main.spy");
@@ -509,8 +528,9 @@ def get_value_b() -> int:
509528
import module_a
510529
import module_b
511530
512-
a = module_a.get_value_a()
513-
b = module_b.get_value_b()
531+
def main():
532+
a = module_a.get_value_a()
533+
b = module_b.get_value_b()
514534
");
515535

516536
helper.WithEntryPoint("main.spy");
@@ -565,7 +585,8 @@ def func_b() -> int:
565585
import a
566586
import b
567587
568-
result = a.func_a() + b.func_b()
588+
def main():
589+
result = a.func_a() + b.func_b()
569590
");
570591

571592
helper.WithEntryPoint("main.spy");
@@ -590,8 +611,9 @@ def subtract(x: int, y: int) -> int:
590611
helper.AddSourceFile("main.spy", @"
591612
import calculator
592613
593-
sum = calculator.add(10, 5)
594-
diff = calculator.subtract(10, 5)
614+
def main():
615+
sum = calculator.add(10, 5)
616+
diff = calculator.subtract(10, 5)
595617
");
596618

597619
helper.WithEntryPoint("main.spy");
@@ -613,8 +635,9 @@ def expects_int(x: int) -> int:
613635
helper.AddSourceFile("main.spy", @"
614636
import typed_module
615637
616-
# Should fail: passing string to int parameter
617-
result = typed_module.expects_int('not an int')
638+
def main():
639+
# Should fail: passing string to int parameter
640+
result = typed_module.expects_int('not an int')
618641
");
619642

620643
helper.WithEntryPoint("main.spy");
@@ -659,9 +682,10 @@ def divide(x: int, y: int) -> int:
659682
helper.AddSourceFile("main.spy", @"
660683
import math_lib
661684
662-
sum = math_lib.add(10, 5)
663-
product = math_lib.multiply(10, 5)
664-
version = math_lib.VERSION
685+
def main():
686+
sum = math_lib.add(10, 5)
687+
product = math_lib.multiply(10, 5)
688+
version = math_lib.VERSION
665689
");
666690

667691
helper.WithEntryPoint("main.spy");
@@ -689,8 +713,9 @@ def format_text(s: str) -> str:
689713
import app.utils
690714
import app.data
691715
692-
text = app.utils.format_text('test')
693-
config = app.data.CONFIG
716+
def main():
717+
text = app.utils.format_text('test')
718+
config = app.data.CONFIG
694719
");
695720

696721
helper.WithEntryPoint("main.spy");
@@ -717,7 +742,8 @@ def func_b() -> int:
717742
import module_a
718743
from module_b import func_b
719744
720-
total = module_a.VALUE_A + func_b()
745+
def main():
746+
total = module_a.VALUE_A + func_b()
721747
");
722748

723749
helper.WithEntryPoint("main.spy");
@@ -753,8 +779,9 @@ def utility() -> str:
753779
import lib
754780
import config
755781
756-
result = lib.utility()
757-
debug = config.DEBUG
782+
def main():
783+
result = lib.utility()
784+
debug = config.DEBUG
758785
");
759786

760787
helper.CreateProjectFile();
@@ -782,11 +809,12 @@ public void ComplexScenario_LargeProjectWithManyFiles_CompilesEfficiently()
782809

783810
// Create main that imports all
784811
var imports = string.Join("\n", Enumerable.Range(0, 10).Select(i => $"import util_{i}"));
785-
var calls = string.Join("\n", Enumerable.Range(0, 10).Select(i => $"val_{i} = util_{i}.func_{i}()"));
812+
var calls = string.Join("\n", Enumerable.Range(0, 10).Select(i => $" val_{i} = util_{i}.func_{i}()"));
786813

787814
helper.AddSourceFile("main.spy", $@"
788815
{imports}
789816
817+
def main():
790818
{calls}
791819
");
792820

@@ -814,7 +842,8 @@ from .. import PARENT_VALUE
814842
helper.AddSourceFile("main.spy", @"
815843
import parent.child
816844
817-
value = parent.child.CHILD_VALUE
845+
def main():
846+
value = parent.child.CHILD_VALUE
818847
");
819848

820849
helper.WithEntryPoint("main.spy");
@@ -835,6 +864,9 @@ public void EdgeCase_EmptyModule_CompilesSuccessfully()
835864
helper.AddSourceFile("empty.spy", "");
836865
helper.AddSourceFile("main.spy", @"
837866
import empty
867+
868+
def main():
869+
pass
838870
");
839871

840872
helper.WithEntryPoint("main.spy");
@@ -855,6 +887,9 @@ public void EdgeCase_ModuleWithOnlyComments_CompilesSuccessfully()
855887

856888
helper.AddSourceFile("main.spy", @"
857889
import comments
890+
891+
def main():
892+
pass
858893
");
859894

860895
helper.WithEntryPoint("main.spy");
@@ -884,8 +919,9 @@ def func() -> int:
884919
import package_a.helper
885920
import package_b.helper
886921
887-
val_a = package_a.helper.func()
888-
val_b = package_b.helper.func()
922+
def main():
923+
val_a = package_a.helper.func()
924+
val_b = package_b.helper.func()
889925
");
890926

891927
helper.WithEntryPoint("main.spy");
@@ -909,7 +945,8 @@ def deep_func() -> str:
909945
helper.AddSourceFile("main.spy", @"
910946
import level1.level2.level3
911947
912-
result = level1.level2.level3.deep_func()
948+
def main():
949+
result = level1.level2.level3.deep_func()
913950
");
914951

915952
helper.WithEntryPoint("main.spy");

0 commit comments

Comments
 (0)