test_pipeline.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import json
  2. import tempfile
  3. import unittest
  4. from pathlib import Path
  5. from unittest.mock import patch
  6. import pipeline as p
  7. class PipelineTests(unittest.TestCase):
  8. def test_pull_before_preparation(self):
  9. with patch.object(p.subprocess, 'check_output', return_value=''), patch.object(p.subprocess, 'run') as run:
  10. p.sync_after_detection()
  11. self.assertEqual(run.call_args.args[0], ['git', 'pull', '--ff-only', 'https://github.com/Light-Beacon/PCL2-NewsHomepage.git', 'main'])
  12. with patch.object(p.subprocess, 'check_output', return_value='local.md'), patch.object(p.subprocess, 'run') as run:
  13. with self.assertRaises(RuntimeError): p.sync_after_detection()
  14. run.assert_not_called()
  15. def test_official_curl_headers_and_error_page(self):
  16. with patch.object(p.subprocess, 'check_output', return_value='<div class="MC_Layout_Article"></div>') as run:
  17. p.fetch_official('https://www.minecraft.net/en-us/article/test')
  18. command = run.call_args.args[0]
  19. self.assertEqual(command[0], 'curl')
  20. self.assertIn('sec-ch-ua-mobile: ?0', command)
  21. self.assertIn('--fail', command)
  22. with patch.object(p.subprocess, 'check_output', return_value='<html>Access Denied</html>'):
  23. with self.assertRaises(RuntimeError): p.fetch_official('https://www.minecraft.net/en-us/article/test')
  24. def test_paths(self):
  25. cases = {'26.3-snapshot-2':'26/26.3/snapshot/26.3-snapshot-2.md',
  26. '1.21.11-pre1':'1.21/1.21.11/pre-release/1.21.11-pre1.md',
  27. '26.1-rc-1':'26/26.1/release-candidate/26.1-rc-1.md',
  28. '1.21.11':'1.21/1.21.11/release/1.21.11.md'}
  29. for vid, expected in cases.items():
  30. self.assertEqual(str(p.version_info(vid)[0]),expected)
  31. with self.assertRaises(ValueError): p.version_info('25w01a')
  32. with self.assertRaises(ValueError): p.version_info('../evil')
  33. def test_apply_preserves_metadata_and_detects_concurrency(self):
  34. with tempfile.TemporaryDirectory() as tmp:
  35. root = Path(tmp).resolve(); lib = root/'Project/libraries/Versions'; lib.mkdir(parents=True)
  36. target = lib/'26.md'
  37. old = '---\nversion-image-link: original\nwip: true\ncats: [26]\n---\n'
  38. target.write_text(old)
  39. packet = root/'packet.json'
  40. packet.write_text(json.dumps({'path':str(target.relative_to(root)),'original_sha256':p.digest(old),'official_url':'https://example.com','wiki_url':'https://wiki.example.com','wiki_revision':123}))
  41. body = '## 新特性\n'+'- 完整中文翻译。\n'*30+'[MC-123](https://example.com)'
  42. (root/'source.md').write_text('[MC-123](https://example.com)')
  43. translation = root/'translation.md'; translation.write_text(body)
  44. with patch.object(p,'ROOT',root),patch.object(p,'LIB',lib):
  45. p.apply(packet,translation)
  46. output = target.read_text()
  47. self.assertIn('version-image-link: original',output)
  48. self.assertNotIn('wip:',output)
  49. self.assertEqual(output.count('llm-transalte: true'),1)
  50. with self.assertRaises(ValueError): p.apply(packet,translation)
  51. target.write_text(old)
  52. translation.write_text('中文'*150)
  53. with self.assertRaisesRegex(ValueError,'bug IDs'): p.apply(packet,translation)
  54. self.assertEqual(target.read_text(),old)
  55. if __name__ == '__main__': unittest.main()