|
| 1 | +/** |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 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 | +**/ |
| 17 | + |
| 18 | +package ldconfig |
| 19 | + |
| 20 | +import ( |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestFilterDirectories(t *testing.T) { |
| 29 | + const topLevelConf = "TOPLEVEL.conf" |
| 30 | + |
| 31 | + testCases := []struct { |
| 32 | + description string |
| 33 | + confs map[string]string // map[filename]content, must have topLevelConf key |
| 34 | + input []string |
| 35 | + expected []string |
| 36 | + }{ |
| 37 | + { |
| 38 | + description: "all filtered", |
| 39 | + confs: map[string]string{ |
| 40 | + topLevelConf: ` |
| 41 | +# some comment |
| 42 | +/tmp/libdir1 |
| 43 | +/tmp/libdir2 |
| 44 | +`, |
| 45 | + }, |
| 46 | + input: []string{"/tmp/libdir1", "/tmp/libdir2"}, |
| 47 | + expected: nil, |
| 48 | + }, |
| 49 | + { |
| 50 | + description: "partially filtered", |
| 51 | + confs: map[string]string{ |
| 52 | + topLevelConf: ` |
| 53 | +/tmp/libdir1 |
| 54 | +`, |
| 55 | + }, |
| 56 | + input: []string{"/tmp/libdir1", "/tmp/libdir2"}, |
| 57 | + expected: []string{"/tmp/libdir2"}, |
| 58 | + }, |
| 59 | + { |
| 60 | + description: "none filtered", |
| 61 | + confs: map[string]string{ |
| 62 | + topLevelConf: ` |
| 63 | +# empty config |
| 64 | +`, |
| 65 | + }, |
| 66 | + input: []string{"/tmp/libdir1", "/tmp/libdir2"}, |
| 67 | + expected: []string{"/tmp/libdir1", "/tmp/libdir2"}, |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "filter with include and comments", |
| 71 | + confs: map[string]string{ |
| 72 | + topLevelConf: ` |
| 73 | +# comment |
| 74 | +/tmp/libdir1 |
| 75 | +include /nonexistent/pattern* |
| 76 | +`, |
| 77 | + }, |
| 78 | + input: []string{"/tmp/libdir1", "/tmp/libdir2"}, |
| 79 | + expected: []string{"/tmp/libdir2"}, |
| 80 | + }, |
| 81 | + { |
| 82 | + description: "include directive picks up more dirs to filter", |
| 83 | + confs: map[string]string{ |
| 84 | + topLevelConf: ` |
| 85 | +# top-level |
| 86 | +include INCLUDED_PATTERN* |
| 87 | +/tmp/libdir3 |
| 88 | +`, |
| 89 | + "INCLUDED_PATTERN0.conf": ` |
| 90 | +/tmp/libdir2 |
| 91 | +# another comment |
| 92 | +/tmp/libdir4 |
| 93 | +`, |
| 94 | + "INCLUDED_PATTERN1.conf": ` |
| 95 | +/tmp/libdir1 |
| 96 | +`, |
| 97 | + }, |
| 98 | + input: []string{"/tmp/libdir1", "/tmp/libdir2", "/tmp/libdir3", "/tmp/libdir4", "/tmp/libdir5"}, |
| 99 | + expected: []string{"/tmp/libdir5"}, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tc := range testCases { |
| 104 | + t.Run(tc.description, func(t *testing.T) { |
| 105 | + tmpDir := t.TempDir() |
| 106 | + |
| 107 | + // Prepare file contents, adjusting include globs to be absolute and unique within tmpDir |
| 108 | + for name, content := range tc.confs { |
| 109 | + if name == topLevelConf && len(tc.confs) > 1 { |
| 110 | + content = strings.ReplaceAll(content, "include INCLUDED_PATTERN*", "include "+tmpDir+"/INCLUDED_PATTERN*") |
| 111 | + } |
| 112 | + err := os.WriteFile(tmpDir+"/"+name, []byte(content), 0600) |
| 113 | + require.NoError(t, err) |
| 114 | + } |
| 115 | + |
| 116 | + topLevelConfPath := tmpDir + "/" + topLevelConf |
| 117 | + filtered, err := filterDirectories(topLevelConfPath, tc.input...) |
| 118 | + |
| 119 | + require.NoError(t, err) |
| 120 | + require.Equal(t, tc.expected, filtered) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments