github.com/nuvolaris/nuv@v0.0.0-20240511174247-a74e3a52bfd8/prepare_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  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,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  package main
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/mitchellh/go-homedir"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func Example_locate() {
    28  	_ = os.Chdir(workDir)
    29  	dir, err := locateNuvRoot("tests")
    30  	pr(1, err, npath(dir))
    31  	dir, err = locateNuvRoot(joinpath("tests", "olaris"))
    32  	pr(2, err, npath(dir))
    33  	dir, err = locateNuvRoot(joinpath("tests", joinpath("olaris", "sub")))
    34  	pr(3, err, npath(dir))
    35  	// Output:
    36  	// 1 <nil> /work/tests/olaris
    37  	// 2 <nil> /work/tests/olaris
    38  	// 3 <nil> /work/tests/olaris
    39  }
    40  
    41  func Example_locate_git() {
    42  	_ = os.Chdir(workDir)
    43  	NuvBranch = "3.0.0-testing"
    44  	nuvdir, _ := homedir.Expand("~/.nuv")
    45  	_ = os.RemoveAll(nuvdir)
    46  	_ = os.Setenv("NUV_BIN", "")
    47  	_, err := locateNuvRoot(".")
    48  	pr(1, err)
    49  	_ = os.Setenv("NUV_BIN", workDir)
    50  	dir, err := locateNuvRoot("tests")
    51  	pr(2, err, npath(dir))
    52  	_, _ = downloadTasksFromGitHub(true, true)
    53  	dir, err = locateNuvRoot(".")
    54  	pr(3, err, nhpath(dir))
    55  	_, _ = downloadTasksFromGitHub(true, true)
    56  	dir, err = locateNuvRoot(".")
    57  	pr(4, err, nhpath(dir))
    58  	os.RemoveAll(nuvdir)
    59  	// Output:
    60  	// 1 we cannot find nuvfiles, download them with nuv -update
    61  	// 2 <nil> /work/tests/olaris
    62  	// Cloning tasks...
    63  	// Nuvfiles downloaded successfully
    64  	// 3 <nil> /home/.nuv/3.0.0-testing/olaris
    65  	// Updating tasks...
    66  	// Tasks are already up to date!
    67  	// 4 <nil> /home/.nuv/3.0.0-testing/olaris
    68  }
    69  
    70  func Test_setNuvOlarisHash(t *testing.T) {
    71  	_ = os.Chdir(workDir)
    72  	NuvBranch = "3.0.0-testing"
    73  	nuvdir, _ := homedir.Expand("~/.nuv")
    74  	_ = os.RemoveAll(nuvdir)
    75  	_ = os.Setenv("NUV_BIN", workDir)
    76  	dir, _ := downloadTasksFromGitHub(true, true)
    77  	err := setNuvOlarisHash(dir)
    78  	require.NoError(t, err)
    79  	require.NotEmpty(t, os.Getenv("NUV_OLARIS"))
    80  }