Skip to content

Commit

Permalink
Merge pull request #35 from cappyzawa/fixable-location
Browse files Browse the repository at this point in the history
Adjust Version.Time for specified location in source configuration.
  • Loading branch information
vito authored Apr 10, 2019
2 parents 102cbfd + 2f5e4f2 commit 95ac3f0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
7 changes: 6 additions & 1 deletion check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ func main() {
previousTime := request.Version.Time
currentTime := time.Now().UTC()

specifiedLocation := request.Source.Location
if specifiedLocation != nil {
currentTime = currentTime.In((*time.Location)(specifiedLocation))
}

tl := lord.TimeLord{
PreviousTime: previousTime,
Location: request.Source.Location,
Location: specifiedLocation,
Start: request.Source.Start,
Stop: request.Source.Stop,
Interval: request.Source.Interval,
Expand Down
13 changes: 13 additions & 0 deletions out/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ package main

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/concourse/time-resource/models"
)

func main() {
var request models.OutRequest

err := json.NewDecoder(os.Stdin).Decode(&request)
if err != nil {
fmt.Fprintln(os.Stderr, "parse error:", err.Error())
os.Exit(1)
}

currentTime := time.Now().UTC()
specifiedLocation := request.Source.Location
if specifiedLocation != nil {
currentTime = currentTime.In((*time.Location)(specifiedLocation))
}

outVersion := models.Version{
Time: currentTime,
Expand Down
53 changes: 38 additions & 15 deletions out/out_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main_test

import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"time"

"github.com/concourse/time-resource/models"
Expand All @@ -20,6 +20,7 @@ var _ = Describe("Out", func() {
var source string

var outCmd *exec.Cmd
var now time.Time

BeforeEach(func() {
var err error
Expand All @@ -30,46 +31,68 @@ var _ = Describe("Out", func() {
source = path.Join(tmpdir, "out-dir")

outCmd = exec.Command(outPath, source)
now = time.Now().UTC()
})

AfterEach(func() {
os.RemoveAll(tmpdir)
})

Context("when executed", func() {
var request models.OutRequest
var source map[string]interface{}
var response models.OutResponse

BeforeEach(func() {
interval := models.Interval(time.Second)

request = models.OutRequest{
Source: models.Source{Interval: &interval},
}

source = map[string]interface{}{}
response = models.OutResponse{}
})

JustBeforeEach(func() {
stdin := new(bytes.Buffer)

err := json.NewEncoder(stdin).Encode(request)
stdin, err := outCmd.StdinPipe()
Expect(err).NotTo(HaveOccurred())

outCmd.Stdin = stdin

session, err := gexec.Start(outCmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())

err = json.NewEncoder(stdin).Encode(map[string]interface{}{
"source": source,
})
Expect(err).NotTo(HaveOccurred())

<-session.Exited
Expect(session.ExitCode()).To(Equal(0))

err = json.Unmarshal(session.Out.Contents(), &response)
Expect(err).NotTo(HaveOccurred())
})

It("reports the current time as the version", func() {
Expect(response.Version.Time).To(BeTemporally("~", time.Now(), time.Second))
Context("when a location is specified", func() {
var loc *time.Location

BeforeEach(func() {
var err error
loc, err = time.LoadLocation("America/Indiana/Indianapolis")
Expect(err).ToNot(HaveOccurred())

source["location"] = loc.String()

now = now.In(loc)
})

It("reports specified location's current time(offset: -0400) as the version", func() {
// An example of response.Version.Time.String() is
// 2019-04-03 14:53:10.951241 -0400 EDT
contained := strings.Contains(response.Version.Time.String(), "-0400")
Expect(contained).To(BeTrue())
})
})
Context("when a location is not specified", func() {
It("reports the current time(offset: 0000) as the version", func() {
// An example of response.Version.Time.String() is
// 2019-04-03 18:53:10.964705 +0000 UTC
contained := strings.Contains(response.Version.Time.String(), "0000")
Expect(contained).To(BeTrue())
})
})
})
})

0 comments on commit 95ac3f0

Please sign in to comment.