github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/metadata/terms_test.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package metadata
    19  
    20  import (
    21  	"bufio"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/mysteriumnetwork/terms/terms-go"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  const notFound = "not found"
    32  
    33  func Test_TermsNotCorrupted(t *testing.T) {
    34  	assert.True(t, len(string(terms.TermsNodeShort)) > 1)
    35  	assert.True(t, len(string(terms.TermsBountyPilot)) > 1)
    36  	assert.True(t, len(string(terms.TermsEndUser)) > 1)
    37  	assert.True(t, len(string(terms.TermsExitNode)) > 1)
    38  
    39  	assert.Equal(t, moduleVersion(t), terms.TermsVersion)
    40  }
    41  
    42  func moduleVersion(t *testing.T) string {
    43  	modFile, err := os.Open("../go.mod")
    44  	assert.NoError(t, err)
    45  
    46  	scanner := bufio.NewScanner(modFile)
    47  	for scanner.Scan() {
    48  		if strings.Contains(scanner.Text(), "github.com/mysteriumnetwork/terms") {
    49  			return parseVersionNoPrefix(scanner.Text())
    50  		}
    51  	}
    52  	return notFound
    53  }
    54  
    55  func parseVersionNoPrefix(line string) string {
    56  	split := strings.Split(strings.TrimSpace(line), " ")
    57  	for _, s := range split {
    58  		if strings.HasPrefix(s, "v") {
    59  			return s[1:]
    60  		}
    61  	}
    62  	return notFound
    63  }