-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdates_check_test.go
134 lines (110 loc) · 3.56 KB
/
updates_check_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package main
import (
"os"
"time"
git "github.com/go-git/go-git/v5"
)
func changeLatestCheckTime(base string, t time.Duration) {
latestCheckPath := joinpath(base, ".latestcheck")
file, err := os.Stat(latestCheckPath)
if err != nil {
pr("failed to get latest_check file info", err)
}
mtime := file.ModTime()
mtime = mtime.Add(t)
err = os.Chtimes(latestCheckPath, mtime, mtime)
if err != nil {
pr("failed to set latest_check file mtime", err)
}
}
func resetOneCommit(repo *git.Repository) {
commIter, _ := repo.Log(&git.LogOptions{
Order: git.LogOrderCommitterTime,
})
if _, err := commIter.Next(); err != nil {
pr("failed to get first commit", err)
}
secondLastCommit, err := commIter.Next()
if err != nil {
pr("failed to get second last commit", err)
}
w, _ := repo.Worktree()
if err := w.Reset(&git.ResetOptions{
Mode: git.HardReset,
Commit: secondLastCommit.Hash,
}); err != nil {
pr("failed to reset repo", err)
}
}
func Example_checkUpdated_uptodate() {
// clone olaris folder into a temp folder
tmpDir, err := os.MkdirTemp("", "nuv-test")
if err != nil {
pr("failed to create temp dir", err)
}
defer os.RemoveAll(tmpDir)
tmpDirBranch := joinpath(tmpDir, getNuvBranch())
olarisTmpPath := joinpath(tmpDirBranch, "olaris")
_, _ = git.PlainClone(olarisTmpPath, false, &git.CloneOptions{
URL: getNuvRepo(),
},
)
// run checkUpdated and check if it creates the latest_check file
createLatestCheckFile(tmpDirBranch)
if exists(tmpDirBranch, ".latestcheck") {
pr("latest_check file created")
}
// change latest_check file mtime to 2 seconds ago
changeLatestCheckTime(tmpDirBranch, -2*time.Second)
// re-run checkUpdated and check output "Tasks up to date!"
checkUpdated(tmpDir, 1*time.Second)
// Output:
// latest_check file created
// Checking for updates...
// Tasks up to date!
}
func Example_checkUpdated_outdated() {
// clone olaris folder into a temp folder
tmpDir, err := os.MkdirTemp("", "nuv-test")
if err != nil {
pr("failed to create temp dir", err)
}
defer os.RemoveAll(tmpDir)
tmpDirBranch := joinpath(tmpDir, getNuvBranch())
olarisTmpPath := joinpath(tmpDirBranch, "olaris")
repo, _ := git.PlainClone(olarisTmpPath, false, &git.CloneOptions{
URL: getNuvRepo(),
},
)
// run checkUpdated and check if it creates the latest_check file
createLatestCheckFile(tmpDirBranch)
if exists(tmpDirBranch, ".latestcheck") {
pr("latest_check file created")
}
// change latest_check file mtime to 2 seconds ago
changeLatestCheckTime(tmpDirBranch, -2*time.Second)
// git reset olaris to a previous commit
resetOneCommit(repo)
// re-run checkUpdated and check output
checkUpdated(tmpDir, 1*time.Second)
// Output:
// latest_check file created
// Checking for updates...
// New tasks available! Use 'nuv -update' to update.
}