blob: 76e928f7a36b500d11149efb3d024770f50f4485 [file] [log] [blame]
Remy Bohmer16c13282020-09-10 10:38:04 +02001# 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 Bohmer16c13282020-09-10 10:38:04 +020017import unittest
18
Mike Frysinger64477332023-08-21 21:20:32 -040019import hooks
20
Gavin Makea2e3302023-03-11 06:46:20 +000021
Remy Bohmer16c13282020-09-10 10:38:04 +020022class RepoHookShebang(unittest.TestCase):
Gavin Makea2e3302023-03-11 06:46:20 +000023 """Check shebang parsing in RepoHook."""
Remy Bohmer16c13282020-09-10 10:38:04 +020024
Gavin Makea2e3302023-03-11 06:46:20 +000025 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 Bohmer16c13282020-09-10 10:38:04 +020030
Gavin Makea2e3302023-03-11 06:46:20 +000031 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 Bohmer16c13282020-09-10 10:38:04 +020044
Gavin Makea2e3302023-03-11 06:46:20 +000045 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 )