github.com/mikejeuga/temperature-converter@v0.0.0-20220721135550-2cf6fcec7145/back-box-tests/acceptancehelpers/cli/cli_client.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mikejeuga/temperature-converter/models"
     6  	"os/exec"
     7  	"strconv"
     8  )
     9  
    10  type TestCliCLient struct {
    11  	fileName string
    12  }
    13  
    14  func NewTestCliCLient(fileName string) *TestCliCLient {
    15  	return &TestCliCLient{
    16  		fileName: fileName,
    17  	}
    18  }
    19  
    20  func (c TestCliCLient) ConvertCtoF(temp models.Celsius) (models.Fahrenheit, error) {
    21  	sprintf := fmt.Sprintf("%v", temp)
    22  	output, err := c.goRun(sprintf)
    23  	if err != nil {
    24  		return 0, err
    25  	}
    26  
    27  	f := string(output)
    28  	floatFahrenheit, err := strconv.ParseFloat(f, 64)
    29  	if err != nil {
    30  		return 0, fmt.Errorf("error converting string to float, %v", err)
    31  	}
    32  
    33  	return models.Fahrenheit(floatFahrenheit), nil
    34  }
    35  
    36  func (c TestCliCLient) ConvertFtoC(temp models.Fahrenheit) (models.Celsius, error) {
    37  	//TODO implement me
    38  	panic("implement me")
    39  }
    40  
    41  func (c TestCliCLient) goRun(value string) ([]byte, error) {
    42  	cmd := exec.Command("go", "run", c.fileName, value)
    43  	output, err := cmd.CombinedOutput()
    44  	if err != nil {
    45  		return nil, fmt.Errorf("error running the fileName built: %v", err)
    46  	}
    47  	return output, nil
    48  }