test_cli_config.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/env python3
  2. """
  3. Comprehensive tests for archivebox config command.
  4. Verify config reads/writes ArchiveBox.conf file correctly.
  5. """
  6. import os
  7. import subprocess
  8. from pathlib import Path
  9. from .fixtures import *
  10. def test_config_displays_all_config(tmp_path, process):
  11. """Test that config without args displays all configuration."""
  12. os.chdir(tmp_path)
  13. result = subprocess.run(['archivebox', 'config'], capture_output=True, text=True)
  14. assert result.returncode == 0
  15. output = result.stdout
  16. # Should show config sections
  17. assert len(output) > 100
  18. # Should show at least some standard config keys
  19. assert 'TIMEOUT' in output or 'OUTPUT_PERMISSIONS' in output
  20. def test_config_get_specific_key(tmp_path, process):
  21. """Test that config --get KEY retrieves specific value."""
  22. os.chdir(tmp_path)
  23. result = subprocess.run(
  24. ['archivebox', 'config', '--get', 'TIMEOUT'],
  25. capture_output=True,
  26. text=True,
  27. )
  28. assert result.returncode == 0
  29. assert 'TIMEOUT' in result.stdout
  30. def test_config_set_writes_to_file(tmp_path, process):
  31. """Test that config --set KEY=VALUE writes to ArchiveBox.conf."""
  32. os.chdir(tmp_path)
  33. result = subprocess.run(
  34. ['archivebox', 'config', '--set', 'TIMEOUT=120'],
  35. capture_output=True,
  36. text=True,
  37. )
  38. assert result.returncode == 0
  39. # Verify config file was updated
  40. config_file = tmp_path / 'ArchiveBox.conf'
  41. assert config_file.exists()
  42. content = config_file.read_text()
  43. assert 'TIMEOUT' in content or '120' in content
  44. def test_config_set_and_get_roundtrip(tmp_path, process):
  45. """Test that set value can be retrieved with get."""
  46. os.chdir(tmp_path)
  47. # Set a unique value
  48. subprocess.run(
  49. ['archivebox', 'config', '--set', 'TIMEOUT=987'],
  50. capture_output=True,
  51. text=True,
  52. )
  53. # Get the value back
  54. result = subprocess.run(
  55. ['archivebox', 'config', '--get', 'TIMEOUT'],
  56. capture_output=True,
  57. text=True,
  58. )
  59. assert '987' in result.stdout
  60. def test_config_set_multiple_values(tmp_path, process):
  61. """Test setting multiple config values at once."""
  62. os.chdir(tmp_path)
  63. result = subprocess.run(
  64. ['archivebox', 'config', '--set', 'TIMEOUT=111', 'YTDLP_TIMEOUT=222'],
  65. capture_output=True,
  66. text=True,
  67. )
  68. assert result.returncode == 0
  69. # Verify both were written
  70. config_file = tmp_path / 'ArchiveBox.conf'
  71. content = config_file.read_text()
  72. assert '111' in content
  73. assert '222' in content
  74. def test_config_set_invalid_key_fails(tmp_path, process):
  75. """Test that setting invalid config key fails."""
  76. os.chdir(tmp_path)
  77. result = subprocess.run(
  78. ['archivebox', 'config', '--set', 'TOTALLY_INVALID_KEY_XYZ=value'],
  79. capture_output=True,
  80. text=True,
  81. )
  82. assert result.returncode != 0
  83. def test_config_set_requires_equals_sign(tmp_path, process):
  84. """Test that set requires KEY=VALUE format."""
  85. os.chdir(tmp_path)
  86. result = subprocess.run(
  87. ['archivebox', 'config', '--set', 'TIMEOUT'],
  88. capture_output=True,
  89. text=True,
  90. )
  91. assert result.returncode != 0
  92. def test_config_search_finds_keys(tmp_path, process):
  93. """Test that config --search finds matching keys."""
  94. os.chdir(tmp_path)
  95. result = subprocess.run(
  96. ['archivebox', 'config', '--search', 'TIMEOUT'],
  97. capture_output=True,
  98. text=True,
  99. )
  100. # Should find timeout-related config
  101. assert 'TIMEOUT' in result.stdout
  102. def test_config_preserves_existing_values(tmp_path, process):
  103. """Test that setting new values preserves existing ones."""
  104. os.chdir(tmp_path)
  105. # Set first value
  106. subprocess.run(
  107. ['archivebox', 'config', '--set', 'TIMEOUT=100'],
  108. capture_output=True,
  109. )
  110. # Set second value
  111. subprocess.run(
  112. ['archivebox', 'config', '--set', 'YTDLP_TIMEOUT=200'],
  113. capture_output=True,
  114. )
  115. # Verify both are in config file
  116. config_file = tmp_path / 'ArchiveBox.conf'
  117. content = config_file.read_text()
  118. assert 'TIMEOUT' in content
  119. assert 'YTDLP_TIMEOUT' in content
  120. def test_config_file_is_valid_toml(tmp_path, process):
  121. """Test that config file remains valid TOML after set."""
  122. os.chdir(tmp_path)
  123. subprocess.run(
  124. ['archivebox', 'config', '--set', 'TIMEOUT=150'],
  125. capture_output=True,
  126. )
  127. config_file = tmp_path / 'ArchiveBox.conf'
  128. content = config_file.read_text()
  129. # Basic TOML validation - should have sections and key=value pairs
  130. assert '[' in content or '=' in content
  131. def test_config_updates_existing_value(tmp_path, process):
  132. """Test that setting same key twice updates the value."""
  133. os.chdir(tmp_path)
  134. # Set initial value
  135. subprocess.run(
  136. ['archivebox', 'config', '--set', 'TIMEOUT=100'],
  137. capture_output=True,
  138. )
  139. # Update to new value
  140. subprocess.run(
  141. ['archivebox', 'config', '--set', 'TIMEOUT=200'],
  142. capture_output=True,
  143. )
  144. # Get current value
  145. result = subprocess.run(
  146. ['archivebox', 'config', '--get', 'TIMEOUT'],
  147. capture_output=True,
  148. text=True,
  149. )
  150. # Should show updated value
  151. assert '200' in result.stdout