github.com/goplus/gop@v1.2.6/env/version_test.go (about)

     1  /*
     2   * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
     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  
    17  package env
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  )
    24  
    25  func TestPanic(t *testing.T) {
    26  	t.Run("initEnvPanic", func(t *testing.T) {
    27  		defer func() {
    28  			if e := recover(); e == nil {
    29  				t.Fatal("initEnvPanic: no panic?")
    30  			}
    31  		}()
    32  		buildVersion = "v1.2"
    33  		initEnv()
    34  	})
    35  	t.Run("GOPROOT panic", func(t *testing.T) {
    36  		defer func() {
    37  			if e := recover(); e == nil {
    38  				t.Fatal("GOPROOT: no panic?")
    39  			}
    40  		}()
    41  		defaultGopRoot = ""
    42  		os.Setenv(envGOPROOT, "")
    43  		GOPROOT()
    44  	})
    45  }
    46  
    47  func TestEnv(t *testing.T) {
    48  	gopEnv = func() (string, error) {
    49  		wd, _ := os.Getwd()
    50  		root := filepath.Dir(wd)
    51  		return "v1.0.0-beta1\n2023-10-18_17-45-50\n" + root + "\n", nil
    52  	}
    53  	buildVersion = ""
    54  	initEnv()
    55  	if !Installed() {
    56  		t.Fatal("not Installed")
    57  	}
    58  	if Version() != "v1.0.0-beta1" {
    59  		t.Fatal("TestVersion failed:", Version())
    60  	}
    61  	buildVersion = ""
    62  	if Version() != "v"+MainVersion+".x" {
    63  		t.Fatal("TestVersion failed:", Version())
    64  	}
    65  	if BuildDate() != "2023-10-18_17-45-50" {
    66  		t.Fatal("BuildInfo failed:", BuildDate())
    67  	}
    68  }