code.gitea.io/gitea@v1.22.3/modules/repository/license_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package repository 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func Test_getLicense(t *testing.T) { 14 type args struct { 15 name string 16 values *LicenseValues 17 } 18 tests := []struct { 19 name string 20 args args 21 want string 22 wantErr assert.ErrorAssertionFunc 23 }{ 24 { 25 name: "regular", 26 args: args{ 27 name: "MIT", 28 values: &LicenseValues{Owner: "Gitea", Year: "2023"}, 29 }, 30 want: `MIT License 31 32 Copyright (c) 2023 Gitea 33 34 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 35 36 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 37 38 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 39 `, 40 wantErr: assert.NoError, 41 }, 42 { 43 name: "license not found", 44 args: args{ 45 name: "notfound", 46 }, 47 wantErr: assert.Error, 48 }, 49 } 50 for _, tt := range tests { 51 t.Run(tt.name, func(t *testing.T) { 52 got, err := GetLicense(tt.args.name, tt.args.values) 53 if !tt.wantErr(t, err, fmt.Sprintf("GetLicense(%v, %v)", tt.args.name, tt.args.values)) { 54 return 55 } 56 assert.Equalf(t, tt.want, string(got), "GetLicense(%v, %v)", tt.args.name, tt.args.values) 57 }) 58 } 59 } 60 61 func Test_fillLicensePlaceholder(t *testing.T) { 62 type args struct { 63 name string 64 values *LicenseValues 65 origin string 66 } 67 tests := []struct { 68 name string 69 args args 70 want string 71 }{ 72 { 73 name: "owner", 74 args: args{ 75 name: "regular", 76 values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"}, 77 origin: ` 78 <name of author> 79 <owner> 80 [NAME] 81 [name of copyright owner] 82 [name of copyright holder] 83 <COPYRIGHT HOLDERS> 84 <copyright holders> 85 <AUTHOR> 86 <author's name or designee> 87 [one or more legally recognised persons or entities offering the Work under the terms and conditions of this Licence] 88 `, 89 }, 90 want: ` 91 Gitea 92 Gitea 93 Gitea 94 Gitea 95 Gitea 96 Gitea 97 Gitea 98 Gitea 99 Gitea 100 Gitea 101 `, 102 }, 103 { 104 name: "email", 105 args: args{ 106 name: "regular", 107 values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"}, 108 origin: ` 109 [EMAIL] 110 `, 111 }, 112 want: ` 113 teabot@gitea.io 114 `, 115 }, 116 { 117 name: "repo", 118 args: args{ 119 name: "regular", 120 values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"}, 121 origin: ` 122 <program> 123 <one line to give the program's name and a brief idea of what it does.> 124 `, 125 }, 126 want: ` 127 gitea 128 gitea 129 `, 130 }, 131 { 132 name: "year", 133 args: args{ 134 name: "regular", 135 values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"}, 136 origin: ` 137 <year> 138 [YEAR] 139 {YEAR} 140 [yyyy] 141 [Year] 142 [year] 143 `, 144 }, 145 want: ` 146 2023 147 2023 148 2023 149 2023 150 2023 151 2023 152 `, 153 }, 154 { 155 name: "0BSD", 156 args: args{ 157 name: "0BSD", 158 values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"}, 159 origin: ` 160 Copyright (C) YEAR by AUTHOR EMAIL 161 162 ... 163 164 ... THE AUTHOR BE LIABLE FOR ... 165 `, 166 }, 167 want: ` 168 Copyright (C) 2023 by Gitea teabot@gitea.io 169 170 ... 171 172 ... THE AUTHOR BE LIABLE FOR ... 173 `, 174 }, 175 } 176 for _, tt := range tests { 177 t.Run(tt.name, func(t *testing.T) { 178 assert.Equalf(t, tt.want, string(fillLicensePlaceholder(tt.args.name, tt.args.values, []byte(tt.args.origin))), "fillLicensePlaceholder(%v, %v, %v)", tt.args.name, tt.args.values, tt.args.origin) 179 }) 180 } 181 }