|
| 1 | +"""Test option removal using 'rm' syntax.""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +def test_option_removal_with_rm(): |
| 9 | + """Test that options can be removed using 'rm' prefix.""" |
| 10 | + test_dir = Path(__file__).parent / "OptionRemoval_Test" |
| 11 | + test_dir.mkdir(exist_ok=True) |
| 12 | + |
| 13 | + # Create template directory with a file containing multiple options |
| 14 | + template_dir = test_dir / "template" |
| 15 | + template_dir.mkdir(exist_ok=True) |
| 16 | + |
| 17 | + template_file = template_dir / "earth.in" |
| 18 | + template_file.write_text("""sName earth |
| 19 | +dMass -1.0 |
| 20 | +dRadius -1.0 |
| 21 | +dSemi -1.0 |
| 22 | +dEcc 0.0 |
| 23 | +dObliquity 23.5 |
| 24 | +""") |
| 25 | + |
| 26 | + # Create vspace input that removes dObliquity using rm syntax |
| 27 | + vspace_in = test_dir / "vspace.in" |
| 28 | + vspace_in.write_text(f"""srcfolder {template_dir} |
| 29 | +destfolder OptionRemovalDest |
| 30 | +samplemode grid |
| 31 | +file earth.in |
| 32 | +dSemi [1.0, 2.0, n2] semi |
| 33 | +rm dObliquity |
| 34 | +""") |
| 35 | + |
| 36 | + # Run vspace with force flag to avoid interactive prompts |
| 37 | + result = subprocess.run( |
| 38 | + ["vspace", "-f", "vspace.in"], |
| 39 | + cwd=test_dir, |
| 40 | + capture_output=True, |
| 41 | + text=True, |
| 42 | + timeout=30 |
| 43 | + ) |
| 44 | + |
| 45 | + assert result.returncode == 0, f"vspace failed: {result.stderr}" |
| 46 | + |
| 47 | + # Check that trials were created |
| 48 | + dest_dir = test_dir / "OptionRemovalDest" |
| 49 | + assert dest_dir.exists() |
| 50 | + |
| 51 | + # Read one of the generated files |
| 52 | + trial_dirs = sorted([d for d in dest_dir.iterdir() if d.is_dir()]) |
| 53 | + assert len(trial_dirs) == 2, f"Expected 2 trials, got {len(trial_dirs)}" |
| 54 | + |
| 55 | + trial_file = trial_dirs[0] / "earth.in" |
| 56 | + assert trial_file.exists() |
| 57 | + |
| 58 | + content = trial_file.read_text() |
| 59 | + |
| 60 | + # Verify dSemi was updated |
| 61 | + assert "dSemi 1.0" in content or "dSemi 2.0" in content |
| 62 | + |
| 63 | + # Verify dObliquity was commented out (removed) |
| 64 | + lines = content.split('\n') |
| 65 | + obliquity_commented = False |
| 66 | + for line in lines: |
| 67 | + if 'dObliquity' in line: |
| 68 | + assert line.strip().startswith('#'), f"dObliquity should be commented: {line}" |
| 69 | + obliquity_commented = True |
| 70 | + |
| 71 | + assert obliquity_commented, "dObliquity should be present but commented out" |
| 72 | + |
| 73 | + # Verify other options remain unchanged |
| 74 | + assert "dMass -1.0" in content |
| 75 | + assert "dRadius -1.0" in content |
| 76 | + |
| 77 | + |
| 78 | +def test_multiple_options_removal(): |
| 79 | + """Test removing multiple options simultaneously.""" |
| 80 | + test_dir = Path(__file__).parent / "MultiOptionRemoval_Test" |
| 81 | + test_dir.mkdir(exist_ok=True) |
| 82 | + |
| 83 | + template_dir = test_dir / "template" |
| 84 | + template_dir.mkdir(exist_ok=True) |
| 85 | + |
| 86 | + template_file = template_dir / "test.in" |
| 87 | + template_file.write_text("""sName test |
| 88 | +dOpt1 1.0 |
| 89 | +dOpt2 2.0 |
| 90 | +dOpt3 3.0 |
| 91 | +dOpt4 4.0 |
| 92 | +dOpt5 5.0 |
| 93 | +""") |
| 94 | + |
| 95 | + vspace_in = test_dir / "vspace.in" |
| 96 | + vspace_in.write_text(f"""srcfolder {template_dir} |
| 97 | +destfolder MultiRemovalDest |
| 98 | +samplemode grid |
| 99 | +file test.in |
| 100 | +dOpt1 [10, 20, n2] opt1 |
| 101 | +rm dOpt2 |
| 102 | +rm dOpt4 |
| 103 | +""") |
| 104 | + |
| 105 | + result = subprocess.run( |
| 106 | + ["vspace", "-f", "vspace.in"], |
| 107 | + cwd=test_dir, |
| 108 | + capture_output=True, |
| 109 | + text=True, |
| 110 | + timeout=30 |
| 111 | + ) |
| 112 | + |
| 113 | + assert result.returncode == 0, f"vspace failed: {result.stderr}" |
| 114 | + |
| 115 | + dest_dir = test_dir / "MultiRemovalDest" |
| 116 | + trial_dirs = sorted([d for d in dest_dir.iterdir() if d.is_dir()]) |
| 117 | + trial_file = trial_dirs[0] / "test.in" |
| 118 | + content = trial_file.read_text() |
| 119 | + |
| 120 | + # dOpt1 should be updated |
| 121 | + assert "dOpt1 10" in content or "dOpt1 20" in content |
| 122 | + |
| 123 | + # dOpt2 and dOpt4 should be commented |
| 124 | + lines = content.split('\n') |
| 125 | + for line in lines: |
| 126 | + if 'dOpt2' in line or 'dOpt4' in line: |
| 127 | + assert line.strip().startswith('#'), f"Option should be commented: {line}" |
| 128 | + |
| 129 | + # dOpt3 and dOpt5 should be unchanged |
| 130 | + assert "dOpt3 3.0" in content |
| 131 | + assert "dOpt5 5.0" in content |
0 commit comments