go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/internal/testutils/mock.go (about)

     1  // Copyright 2023 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 testutils
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/cipkg/core"
    23  )
    24  
    25  var (
    26  	_ core.PackageManager = &MockPackageManager{}
    27  	_ core.PackageHandler = &MockPackageHandler{}
    28  )
    29  
    30  // MockPackageManager implements core.PackageManager interface. It stores
    31  // metadata and derivation in the memory. It doesn't allocate any "real" storage
    32  // in the filesystem.
    33  type MockPackageManager struct {
    34  	pkgs    map[string]core.PackageHandler
    35  	baseDir string
    36  }
    37  
    38  func NewMockPackageManage(tempDir string) core.PackageManager {
    39  	return &MockPackageManager{
    40  		pkgs:    make(map[string]core.PackageHandler),
    41  		baseDir: tempDir,
    42  	}
    43  }
    44  
    45  func (pm *MockPackageManager) Get(id string) core.PackageHandler {
    46  	if h, ok := pm.pkgs[id]; ok {
    47  		return h
    48  	}
    49  	h := &MockPackageHandler{
    50  		id:        id,
    51  		available: false,
    52  		baseDir:   filepath.Join(pm.baseDir, "pkgs", id),
    53  	}
    54  	pm.pkgs[id] = h
    55  	return h
    56  }
    57  
    58  type MockPackageHandler struct {
    59  	id        string
    60  	available bool
    61  
    62  	lastUsed time.Time
    63  	ref      int
    64  
    65  	baseDir string
    66  }
    67  
    68  func (p *MockPackageHandler) OutputDirectory() string {
    69  	return filepath.Join(p.baseDir, "content")
    70  }
    71  func (p *MockPackageHandler) LoggingDirectory() string {
    72  	return filepath.Join(p.baseDir, "logs")
    73  }
    74  func (p *MockPackageHandler) Build(f func() error) error {
    75  	if err := f(); err != nil {
    76  		return err
    77  	}
    78  	p.available = true
    79  	return nil
    80  }
    81  
    82  func (p *MockPackageHandler) TryRemove() (ok bool, err error) {
    83  	if !p.available || p.ref != 0 {
    84  		return false, nil
    85  	}
    86  	p.available = false
    87  	return true, nil
    88  }
    89  
    90  func (p *MockPackageHandler) IncRef() error {
    91  	p.ref += 1
    92  	p.lastUsed = time.Now()
    93  	return nil
    94  }
    95  
    96  func (p *MockPackageHandler) DecRef() error {
    97  	if p.ref == 0 {
    98  		return fmt.Errorf("no reference to the package")
    99  	}
   100  	p.ref -= 1
   101  	return nil
   102  }