1
1
mirror of https://github.com/walles/moar.git synced 2024-10-05 16:07:54 +03:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Johan Walles
831c894e69
Merge pull request #220 from walles/johan/windows-readable-executable
Fix executability check on Windows
2024-06-29 08:41:08 +02:00
Johan Walles
2e13403096 Fix executability check on Windows 2024-06-29 08:36:13 +02:00
Johan Walles
a5317483e8 Test our executability checker 2024-06-29 08:24:15 +02:00
Johan Walles
8dd02a3fe1 Extract checks to be testable 2024-06-28 22:37:34 +02:00
3 changed files with 61 additions and 19 deletions

View File

@ -1,9 +1,11 @@
package m
import (
"fmt"
"math"
"os"
"os/exec"
"runtime"
"strings"
log "github.com/sirupsen/logrus"
@ -104,6 +106,27 @@ func dumpToTempFile(reader *Reader) (string, error) {
return tempFile.Name(), nil
}
// Check that the editor is executable
func errUnlessExecutable(file string) error {
stat, err := os.Stat(file)
if err != nil {
return fmt.Errorf("Failed to stat %s: %w", file, err)
}
if runtime.GOOS == "windows" && strings.HasSuffix(strings.ToLower(file), ".exe") {
log.Debug(".exe file on Windows, assuming executable: ", file)
return nil
}
if stat.Mode()&0111 != 0 {
// Note that this check isn't perfect, it could still be executable but
// not by us. Corner case, let's just fail later in that case.
return nil
}
return fmt.Errorf("Not executable: %s", file)
}
func handleEditingRequest(p *Pager) {
// Get an editor setting from either VISUAL or EDITOR
editorEnv := "VISUAL"
@ -129,37 +152,23 @@ func handleEditingRequest(p *Pager) {
log.Warn("Failed to find editor "+firstWord+" from $"+editorEnv+": ", err)
return
}
// Check that the editor is executable
editorStat, err := os.Stat(editorPath)
err = errUnlessExecutable(editorPath)
if err != nil {
// FIXME: Show a message in the status bar instead? Nothing wrong with
// moar here.
log.Warn("Failed to stat editor "+editorPath+": ", err)
return
}
if editorStat.Mode()&0111 == 0 {
// Note that this check isn't perfect, it could still be executable but
// not by us. Corner case, let's just fail later in that case.
// FIXME: Show a message in the status bar instead? Nothing wrong with
// moar here.
log.Warn("Editor " + editorPath + " is not executable")
log.Warn("Editor not executable: {}", err)
return
}
canOpenFile := p.reader.fileName != nil
if p.reader.fileName != nil {
// Verify that the file exists and is readable
fileToEditStat, err := os.Stat(*p.reader.fileName)
err = tryOpen(*p.reader.fileName)
if err != nil {
log.Info("Failed to stat file to edit "+*p.reader.fileName+": ", err)
canOpenFile = false
} else if fileToEditStat.Mode()&0444 == 0 {
// Note that this check isn't perfect, it could still be readable but
// not by us. Corner case, let's just fail later in that case.
log.Info("File to edit " + *p.reader.fileName + " is not readable")
canOpenFile = false
log.Info("File to edit is not readable: ", err)
}
}

View File

@ -0,0 +1,32 @@
package m
import (
"os"
"testing"
)
func TestErrUnlessExecutable_yes(t *testing.T) {
// Find our own executable
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
// Check that it's executable
err = errUnlessExecutable(executable)
if err != nil {
t.Fatal(err)
}
}
func TestErrUnlessExecutable_no(t *testing.T) {
textFile := "pagermode-viewing_test.go"
if _, err := os.Stat(textFile); os.IsNotExist(err) {
t.Fatal("Test setup failed, text file not found: " + textFile)
}
err := errUnlessExecutable(textFile)
if err == nil {
t.Fatal("Expected error, got nil")
}
}

View File

@ -280,6 +280,7 @@ func printProblemsHeader() {
fmt.Fprintln(os.Stderr, "LANG :", os.Getenv("LANG"))
fmt.Fprintln(os.Stderr, "TERM :", os.Getenv("TERM"))
fmt.Fprintln(os.Stderr, "MOAR :", os.Getenv("MOAR"))
fmt.Fprintln(os.Stderr, "EDITOR :", os.Getenv("EDITOR"))
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "GOOS :", runtime.GOOS)
fmt.Fprintln(os.Stderr, "GOARCH :", runtime.GOARCH)