github.com/ianlewis/go-gitignore@v0.1.1-0.20231110021210-4a0f15cbd56f/util_test.go (about) 1 // Copyright 2016 Denormal Limited 2 // Copyright 2023 Google LLC 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package gitignore_test 17 18 import ( 19 "bytes" 20 "fmt" 21 "io" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 "strings" 26 27 "github.com/ianlewis/go-gitignore" 28 ) 29 30 func file(content string) (*os.File, error) { 31 // create a temporary file 32 _file, _err := ioutil.TempFile("", "gitignore") 33 if _err != nil { 34 return nil, _err 35 } 36 37 // populate this file with the example .gitignore 38 _, _err = _file.WriteString(content) 39 if _err != nil { 40 defer os.Remove(_file.Name()) 41 return nil, _err 42 } 43 _, _err = _file.Seek(0, io.SeekStart) 44 if _err != nil { 45 defer os.Remove(_file.Name()) 46 return nil, _err 47 } 48 49 // we have a temporary file containing the .gitignore 50 return _file, nil 51 } // file() 52 53 func dir(content map[string]string) (string, error) { 54 // create a temporary directory 55 _dir, _err := ioutil.TempDir("", "") 56 if _err != nil { 57 return "", _err 58 } 59 60 // resolve the path of this directory 61 // - we do this to handle systems with a temporary directory 62 // that is a symbolic link 63 _dir, _err = filepath.EvalSymlinks(_dir) 64 if _err != nil { 65 defer os.RemoveAll(_dir) 66 return "", _err 67 } 68 69 // Return early if there is no content. 70 if content == nil { 71 // return the temporary directory name 72 return _dir, nil 73 } 74 75 // populate the temporary directory with the content map 76 // - each key of the map is a file name 77 // - each value of the map is the file content 78 // - file names are relative to the temporary directory 79 for _key, _content := range content { 80 // ensure we have content to store 81 if _content == "" { 82 continue 83 } 84 85 // should we create a directory or a file? 86 _isdir := false 87 _path := _key 88 if strings.HasSuffix(_path, "/") { 89 _path = strings.TrimSuffix(_path, "/") 90 _isdir = true 91 } 92 93 // construct the absolute path (according to the local file system) 94 _abs := _dir 95 _parts := strings.Split(_path, "/") 96 _last := len(_parts) - 1 97 if _isdir { 98 _abs = filepath.Join(_abs, filepath.Join(_parts...)) 99 } else if _last > 0 { 100 _abs = filepath.Join(_abs, filepath.Join(_parts[:_last]...)) 101 } 102 103 // ensure this directory exists 104 _err = os.MkdirAll(_abs, _GITMASK) 105 if _err != nil { 106 defer os.RemoveAll(_dir) 107 return "", _err 108 } else if _isdir { 109 continue 110 } 111 112 // create the absolute path for the target file 113 _abs = filepath.Join(_abs, _parts[_last]) 114 115 // write the contents to this file 116 _file, _err := os.Create(_abs) 117 if _err != nil { 118 defer os.RemoveAll(_dir) 119 return "", _err 120 } 121 _, _err = _file.WriteString(_content) 122 if _err != nil { 123 defer os.RemoveAll(_dir) 124 return "", _err 125 } 126 _err = _file.Close() 127 if _err != nil { 128 defer os.RemoveAll(_dir) 129 return "", _err 130 } 131 } 132 133 // return the temporary directory name 134 return _dir, nil 135 } // dir() 136 137 func exclude(content string) (string, error) { 138 // create a temporary folder with the info/ subfolder 139 _dir, _err := dir(nil) 140 if _err != nil { 141 return "", _err 142 } 143 _info := filepath.Join(_dir, "info") 144 _err = os.MkdirAll(_info, _GITMASK) 145 if _err != nil { 146 defer os.RemoveAll(_dir) 147 return "", _err 148 } 149 150 // create the exclude file 151 _exclude := filepath.Join(_info, "exclude") 152 _err = ioutil.WriteFile(_exclude, []byte(content), _GITMASK) 153 if _err != nil { 154 defer os.RemoveAll(_dir) 155 return "", _err 156 } 157 158 // return the temporary directory name 159 return _dir, nil 160 } // exclude() 161 162 func coincident(a, b gitignore.Position) bool { 163 return a.File == b.File && 164 a.Line == b.Line && 165 a.Column == b.Column && 166 a.Offset == b.Offset 167 } // coincident() 168 169 func pos(p gitignore.Position) string { 170 _prefix := p.File 171 if _prefix != "" { 172 _prefix = _prefix + ": " 173 } 174 175 return fmt.Sprintf("%s%d:%d [%d]", _prefix, p.Line, p.Column, p.Offset) 176 } // pos() 177 178 func buffer(content string) (*bytes.Buffer, error) { 179 // return a buffered .gitignore 180 return bytes.NewBufferString(content), nil 181 } // buffer() 182 183 func null() gitignore.GitIgnore { 184 // return an empty GitIgnore instance 185 return gitignore.New(bytes.NewBuffer(nil), "", nil) 186 } // null()