test_hooks.py 1.9 KB

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