test_hooks.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2019 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Unittests for the hooks.py module."""
  15. import hooks
  16. import unittest
  17. class RepoHookShebang(unittest.TestCase):
  18. """Check shebang parsing in RepoHook."""
  19. def test_no_shebang(self):
  20. """Lines w/out shebangs should be rejected."""
  21. DATA = (
  22. '',
  23. '#\n# foo\n',
  24. '# Bad shebang in script\n#!/foo\n'
  25. )
  26. for data in DATA:
  27. self.assertIsNone(hooks.RepoHook._ExtractInterpFromShebang(data))
  28. def test_direct_interp(self):
  29. """Lines whose shebang points directly to the interpreter."""
  30. DATA = (
  31. ('#!/foo', '/foo'),
  32. ('#! /foo', '/foo'),
  33. ('#!/bin/foo ', '/bin/foo'),
  34. ('#! /usr/foo ', '/usr/foo'),
  35. ('#! /usr/foo -args', '/usr/foo'),
  36. )
  37. for shebang, interp in DATA:
  38. self.assertEqual(hooks.RepoHook._ExtractInterpFromShebang(shebang),
  39. interp)
  40. def test_env_interp(self):
  41. """Lines whose shebang launches through `env`."""
  42. DATA = (
  43. ('#!/usr/bin/env foo', 'foo'),
  44. ('#!/bin/env foo', 'foo'),
  45. ('#! /bin/env /bin/foo ', '/bin/foo'),
  46. )
  47. for shebang, interp in DATA:
  48. self.assertEqual(hooks.RepoHook._ExtractInterpFromShebang(shebang),
  49. interp)