test_project.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. import unittest
  17. import project
  18. class RepoHookShebang(unittest.TestCase):
  19. """Check shebang parsing in RepoHook."""
  20. def test_no_shebang(self):
  21. """Lines w/out shebangs should be rejected."""
  22. DATA = (
  23. '',
  24. '# -*- coding:utf-8 -*-\n',
  25. '#\n# foo\n',
  26. '# Bad shebang in script\n#!/foo\n'
  27. )
  28. for data in DATA:
  29. self.assertIsNone(project.RepoHook._ExtractInterpFromShebang(data))
  30. def test_direct_interp(self):
  31. """Lines whose shebang points directly to the interpreter."""
  32. DATA = (
  33. ('#!/foo', '/foo'),
  34. ('#! /foo', '/foo'),
  35. ('#!/bin/foo ', '/bin/foo'),
  36. ('#! /usr/foo ', '/usr/foo'),
  37. ('#! /usr/foo -args', '/usr/foo'),
  38. )
  39. for shebang, interp in DATA:
  40. self.assertEqual(project.RepoHook._ExtractInterpFromShebang(shebang),
  41. interp)
  42. def test_env_interp(self):
  43. """Lines whose shebang launches through `env`."""
  44. DATA = (
  45. ('#!/usr/bin/env foo', 'foo'),
  46. ('#!/bin/env foo', 'foo'),
  47. ('#! /bin/env /bin/foo ', '/bin/foo'),
  48. )
  49. for shebang, interp in DATA:
  50. self.assertEqual(project.RepoHook._ExtractInterpFromShebang(shebang),
  51. interp)