test_bump_action.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import subprocess
  2. from unittest import mock
  3. import pytest
  4. from tools.bump_action import main
  5. @pytest.fixture
  6. def workflow_and_action(tmp_path):
  7. base = tmp_path.joinpath("root")
  8. base.joinpath(".github/workflows").mkdir(parents=True)
  9. workflow = base.joinpath(".github/workflows/main.yml")
  10. base.joinpath(".github/actions/myaction").mkdir(parents=True)
  11. action = base.joinpath(".github/actions/myaction/action.yml")
  12. yield base, workflow, action
  13. def test_main_noop(workflow_and_action, capsys):
  14. base, workflow, action = workflow_and_action
  15. workflow_src = """\
  16. name: main
  17. on:
  18. push:
  19. jobs:
  20. main:
  21. runs-on: ubuntu-latest
  22. steps:
  23. - run: echo hi
  24. """
  25. action_src = """\
  26. name: my-action
  27. inputs:
  28. arg:
  29. required: true
  30. runs:
  31. using: composite
  32. steps:
  33. - run: echo hi
  34. shell: bash
  35. """
  36. workflow.write_text(workflow_src)
  37. action.write_text(action_src)
  38. assert main(("actions/whatever", "v1.2.3", f"--base-dir={base}")) == 0
  39. out, err = capsys.readouterr()
  40. assert out == err == ""
  41. def test_main_upgrades_action(workflow_and_action, capsys):
  42. base, workflow, action = workflow_and_action
  43. workflow_src = """\
  44. name: main
  45. on:
  46. push:
  47. jobs:
  48. main:
  49. runs-on: ubuntu-latest
  50. steps:
  51. - uses: actions/whatever@v0.1.2
  52. """
  53. workflow_expected = """\
  54. name: main
  55. on:
  56. push:
  57. jobs:
  58. main:
  59. runs-on: ubuntu-latest
  60. steps:
  61. - uses: actions/whatever@v1.2.3
  62. """
  63. action_src = """\
  64. name: my-action
  65. inputs:
  66. arg:
  67. required: true
  68. runs:
  69. using: composite
  70. steps:
  71. - uses: actions/whatever@v0.1.2
  72. """
  73. action_expected = """\
  74. name: my-action
  75. inputs:
  76. arg:
  77. required: true
  78. runs:
  79. using: composite
  80. steps:
  81. - uses: actions/whatever@v1.2.3
  82. """
  83. workflow.write_text(workflow_src)
  84. action.write_text(action_src)
  85. with mock.patch.object(subprocess, "call", return_value=123):
  86. assert main(("actions/whatever", "v1.2.3", f"--base-dir={base}")) == 123
  87. out, err = capsys.readouterr()
  88. assert (
  89. out
  90. == f"""\
  91. {workflow} upgrading actions/whatever...
  92. {action} upgrading actions/whatever...
  93. freezing...
  94. """
  95. )
  96. assert workflow.read_text() == workflow_expected
  97. assert action.read_text() == action_expected