test_package_skill.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env python3
  2. """
  3. Tests for cli/package_skill.py functionality
  4. """
  5. import unittest
  6. import tempfile
  7. import zipfile
  8. from pathlib import Path
  9. import sys
  10. from skill_seekers.cli.package_skill import package_skill
  11. class TestPackageSkill(unittest.TestCase):
  12. """Test package_skill function"""
  13. def create_test_skill_directory(self, tmpdir):
  14. """Helper to create a test skill directory structure"""
  15. skill_dir = Path(tmpdir) / "test-skill"
  16. skill_dir.mkdir()
  17. # Create SKILL.md
  18. (skill_dir / "SKILL.md").write_text("---\nname: test-skill\n---\n# Test Skill")
  19. # Create references directory
  20. refs_dir = skill_dir / "references"
  21. refs_dir.mkdir()
  22. (refs_dir / "index.md").write_text("# Index")
  23. (refs_dir / "getting_started.md").write_text("# Getting Started")
  24. # Create scripts directory (empty)
  25. (skill_dir / "scripts").mkdir()
  26. # Create assets directory (empty)
  27. (skill_dir / "assets").mkdir()
  28. return skill_dir
  29. def test_package_valid_skill_directory(self):
  30. """Test packaging a valid skill directory"""
  31. with tempfile.TemporaryDirectory() as tmpdir:
  32. skill_dir = self.create_test_skill_directory(tmpdir)
  33. success, zip_path = package_skill(skill_dir, open_folder_after=False, skip_quality_check=True)
  34. self.assertTrue(success)
  35. self.assertIsNotNone(zip_path)
  36. self.assertTrue(zip_path.exists())
  37. self.assertEqual(zip_path.suffix, '.zip')
  38. self.assertTrue(zipfile.is_zipfile(zip_path))
  39. def test_package_creates_correct_zip_structure(self):
  40. """Test that packaged zip contains correct files"""
  41. with tempfile.TemporaryDirectory() as tmpdir:
  42. skill_dir = self.create_test_skill_directory(tmpdir)
  43. success, zip_path = package_skill(skill_dir, open_folder_after=False, skip_quality_check=True)
  44. self.assertTrue(success)
  45. # Check zip contents
  46. with zipfile.ZipFile(zip_path, 'r') as zf:
  47. names = zf.namelist()
  48. # Should contain SKILL.md
  49. self.assertTrue(any('SKILL.md' in name for name in names))
  50. # Should contain references
  51. self.assertTrue(any('references/index.md' in name for name in names))
  52. self.assertTrue(any('references/getting_started.md' in name for name in names))
  53. def test_package_excludes_backup_files(self):
  54. """Test that .backup files are excluded from zip"""
  55. with tempfile.TemporaryDirectory() as tmpdir:
  56. skill_dir = self.create_test_skill_directory(tmpdir)
  57. # Add a backup file
  58. (skill_dir / "SKILL.md.backup").write_text("# Backup")
  59. success, zip_path = package_skill(skill_dir, open_folder_after=False, skip_quality_check=True)
  60. self.assertTrue(success)
  61. # Check that backup is NOT in zip
  62. with zipfile.ZipFile(zip_path, 'r') as zf:
  63. names = zf.namelist()
  64. self.assertFalse(any('.backup' in name for name in names))
  65. def test_package_nonexistent_directory(self):
  66. """Test packaging a nonexistent directory"""
  67. success, zip_path = package_skill("/nonexistent/path", open_folder_after=False, skip_quality_check=True)
  68. self.assertFalse(success)
  69. self.assertIsNone(zip_path)
  70. def test_package_directory_without_skill_md(self):
  71. """Test packaging directory without SKILL.md"""
  72. with tempfile.TemporaryDirectory() as tmpdir:
  73. skill_dir = Path(tmpdir) / "invalid-skill"
  74. skill_dir.mkdir()
  75. success, zip_path = package_skill(skill_dir, open_folder_after=False, skip_quality_check=True)
  76. self.assertFalse(success)
  77. self.assertIsNone(zip_path)
  78. def test_package_creates_zip_in_correct_location(self):
  79. """Test that zip is created in output/ directory"""
  80. with tempfile.TemporaryDirectory() as tmpdir:
  81. # Create skill in output-like structure
  82. output_dir = Path(tmpdir) / "output"
  83. output_dir.mkdir()
  84. skill_dir = output_dir / "test-skill"
  85. skill_dir.mkdir()
  86. (skill_dir / "SKILL.md").write_text("# Test")
  87. (skill_dir / "references").mkdir()
  88. (skill_dir / "scripts").mkdir()
  89. (skill_dir / "assets").mkdir()
  90. success, zip_path = package_skill(skill_dir, open_folder_after=False, skip_quality_check=True)
  91. self.assertTrue(success)
  92. # Zip should be in output directory, not inside skill directory
  93. self.assertEqual(zip_path.parent, output_dir)
  94. self.assertEqual(zip_path.name, "test-skill.zip")
  95. def test_package_zip_name_matches_skill_name(self):
  96. """Test that zip filename matches skill directory name"""
  97. with tempfile.TemporaryDirectory() as tmpdir:
  98. skill_dir = Path(tmpdir) / "my-awesome-skill"
  99. skill_dir.mkdir()
  100. (skill_dir / "SKILL.md").write_text("# Test")
  101. (skill_dir / "references").mkdir()
  102. (skill_dir / "scripts").mkdir()
  103. (skill_dir / "assets").mkdir()
  104. success, zip_path = package_skill(skill_dir, open_folder_after=False, skip_quality_check=True)
  105. self.assertTrue(success)
  106. self.assertEqual(zip_path.name, "my-awesome-skill.zip")
  107. class TestPackageSkillCLI(unittest.TestCase):
  108. """Test package_skill.py command-line interface"""
  109. def test_cli_help_output(self):
  110. """Test that skill-seekers package --help works"""
  111. import subprocess
  112. try:
  113. result = subprocess.run(
  114. ['skill-seekers', 'package', '--help'],
  115. capture_output=True,
  116. text=True,
  117. timeout=5
  118. )
  119. # argparse may return 0 or 2 for --help
  120. self.assertIn(result.returncode, [0, 2])
  121. output = result.stdout + result.stderr
  122. self.assertTrue('usage:' in output.lower() or 'package' in output.lower())
  123. except FileNotFoundError:
  124. self.skipTest("skill-seekers command not installed")
  125. def test_cli_executes_without_errors(self):
  126. """Test that skill-seekers-package entry point works"""
  127. import subprocess
  128. try:
  129. result = subprocess.run(
  130. ['skill-seekers-package', '--help'],
  131. capture_output=True,
  132. text=True,
  133. timeout=5
  134. )
  135. # argparse may return 0 or 2 for --help
  136. self.assertIn(result.returncode, [0, 2])
  137. except FileNotFoundError:
  138. self.skipTest("skill-seekers-package command not installed")
  139. if __name__ == '__main__':
  140. unittest.main()