go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/cmd/cas/casimpl/archive_test.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package casimpl
    16  
    17  import (
    18  	"encoding/json"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestGetRoot(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	Convey(`Basic`, t, func() {
    30  		dirs := make(scatterGather)
    31  		So(dirs.Add("wd1", "rel"), ShouldBeNil)
    32  		So(dirs.Add("wd1", "rel2"), ShouldBeNil)
    33  
    34  		wd, err := getRoot(dirs)
    35  		So(err, ShouldBeNil)
    36  		So(wd, ShouldEqual, "wd1")
    37  
    38  		So(dirs.Add("wd2", "rel3"), ShouldBeNil)
    39  
    40  		_, err = getRoot(dirs)
    41  		So(err, ShouldNotBeNil)
    42  	})
    43  }
    44  
    45  func TestLoadPathsJSON(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	Convey(`Basic`, t, func() {
    49  		dir := t.TempDir()
    50  		input := [][2]string{
    51  			{dir, "foo.txt"},
    52  			{dir, "bar.txt"},
    53  		}
    54  		b, err := json.Marshal(input)
    55  		So(err, ShouldBeNil)
    56  
    57  		pathsJSON := filepath.Join(dir, "paths.json")
    58  		err = os.WriteFile(pathsJSON, b, 0o600)
    59  		So(err, ShouldBeNil)
    60  
    61  		res, err := loadPathsJSON(pathsJSON)
    62  		So(err, ShouldBeNil)
    63  
    64  		So(res, ShouldResemble, scatterGather{
    65  			"foo.txt": dir,
    66  			"bar.txt": dir,
    67  		})
    68  	})
    69  }