github.com/wtsi-hgi/go-softpack-builder@v1.8.1/internal/buildermock/mock.go (about)

     1  /*******************************************************************************
     2   * Copyright (c) 2024 Genome Research Ltd.
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining
     5   * a copy of this software and associated documentation files (the
     6   * "Software"), to deal in the Software without restriction, including
     7   * without limitation the rights to use, copy, modify, merge, publish,
     8   * distribute, sublicense, and/or sell copies of the Software, and to
     9   * permit persons to whom the Software is furnished to do so, subject to
    10   * the following conditions:
    11   *
    12   * The above copyright notice and this permission notice shall be included
    13   * in all copies or substantial portions of the Software.
    14   *
    15   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    16   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    17   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    18   * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    19   * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    20   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    21   * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22   ******************************************************************************/
    23  
    24  package buildermock
    25  
    26  import (
    27  	"path/filepath"
    28  	"time"
    29  
    30  	"github.com/wtsi-hgi/go-softpack-builder/build"
    31  )
    32  
    33  // MockBuilder can be used to test a server.Server without having real builder.
    34  type MockBuilder struct {
    35  	Received  []*build.Definition
    36  	Requested []time.Time
    37  }
    38  
    39  // Build adds the given def to our slice of Received.
    40  func (m *MockBuilder) Build(def *build.Definition) error { //nolint:unparam
    41  	m.Received = append(m.Received, def)
    42  
    43  	return nil
    44  }
    45  
    46  // Status returns a status for everything sent to Build, assuming you pushed
    47  // a corresponding timestamp to our Requested slice manually.
    48  func (m *MockBuilder) Status() []build.Status {
    49  	statuses := make([]build.Status, 0, len(m.Received))
    50  
    51  	for i, def := range m.Received {
    52  		if len(m.Requested) <= i {
    53  			break
    54  		}
    55  
    56  		statuses = append(statuses, build.Status{
    57  			Name:      filepath.Join(def.EnvironmentPath, def.EnvironmentName) + "-" + def.EnvironmentVersion,
    58  			Requested: &m.Requested[i],
    59  		})
    60  	}
    61  
    62  	return statuses
    63  }