Remy Bohmer | 16c1328 | 2020-09-10 10:38:04 +0200 | [diff] [blame] | 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 | |
| 15 | """Unittests for the hooks.py module.""" |
| 16 | |
Remy Bohmer | 16c1328 | 2020-09-10 10:38:04 +0200 | [diff] [blame] | 17 | import unittest |
| 18 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 19 | import hooks |
| 20 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 21 | |
Remy Bohmer | 16c1328 | 2020-09-10 10:38:04 +0200 | [diff] [blame] | 22 | class RepoHookShebang(unittest.TestCase): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 23 | """Check shebang parsing in RepoHook.""" |
Remy Bohmer | 16c1328 | 2020-09-10 10:38:04 +0200 | [diff] [blame] | 24 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 25 | def test_no_shebang(self): |
| 26 | """Lines w/out shebangs should be rejected.""" |
| 27 | DATA = ("", "#\n# foo\n", "# Bad shebang in script\n#!/foo\n") |
| 28 | for data in DATA: |
| 29 | self.assertIsNone(hooks.RepoHook._ExtractInterpFromShebang(data)) |
Remy Bohmer | 16c1328 | 2020-09-10 10:38:04 +0200 | [diff] [blame] | 30 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 31 | def test_direct_interp(self): |
| 32 | """Lines whose shebang points directly to the interpreter.""" |
| 33 | DATA = ( |
| 34 | ("#!/foo", "/foo"), |
| 35 | ("#! /foo", "/foo"), |
| 36 | ("#!/bin/foo ", "/bin/foo"), |
| 37 | ("#! /usr/foo ", "/usr/foo"), |
| 38 | ("#! /usr/foo -args", "/usr/foo"), |
| 39 | ) |
| 40 | for shebang, interp in DATA: |
| 41 | self.assertEqual( |
| 42 | hooks.RepoHook._ExtractInterpFromShebang(shebang), interp |
| 43 | ) |
Remy Bohmer | 16c1328 | 2020-09-10 10:38:04 +0200 | [diff] [blame] | 44 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 45 | def test_env_interp(self): |
| 46 | """Lines whose shebang launches through `env`.""" |
| 47 | DATA = ( |
| 48 | ("#!/usr/bin/env foo", "foo"), |
| 49 | ("#!/bin/env foo", "foo"), |
| 50 | ("#! /bin/env /bin/foo ", "/bin/foo"), |
| 51 | ) |
| 52 | for shebang, interp in DATA: |
| 53 | self.assertEqual( |
| 54 | hooks.RepoHook._ExtractInterpFromShebang(shebang), interp |
| 55 | ) |