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