Files

114 lines
4.0 KiB
Go

package main
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func writeFixture(t *testing.T, path, text string) {
t.Helper()
if e := os.MkdirAll(filepath.Dir(path), 0700); e != nil {
t.Fatal(e)
}
if e := os.WriteFile(path, []byte(text), 0600); e != nil {
t.Fatal(e)
}
}
func checkStatus(t *testing.T, p Preflight, name, want string) {
t.Helper()
for _, item := range p.Items {
if item.Name == name {
if item.Status != want {
t.Fatalf("%s = %s, want %s: %s", name, item.Status, want, item.Message)
}
return
}
}
t.Fatalf("missing check %s: %+v", name, p)
}
func TestSDKLiteralRequirements(t *testing.T) {
work := t.TempDir()
writeFixture(t, filepath.Join(work, "app", "build.gradle"), `plugins { id 'com.android.application' }
// compileSdk 99
/* ndkVersion "1.0.0" */
android { compileSdkVersion 36
buildToolsVersion '36.0.0'
ndkVersion = "27.0.12077973"
}`)
writeFixture(t, filepath.Join(work, "lib", "build.gradle.kts"), `android { compileSdk = 36
ndkVersion = libs.versions.ndk.get()
}`)
writeFixture(t, filepath.Join(work, "app", "build", "build.gradle"), `compileSdk 100`)
packages, android, e := requiredSDKPackages(work)
if e != nil || !android || strings.Join(packages, ",") != "platforms;android-36,build-tools;36.0.0,ndk;27.0.12077973" {
t.Fatalf("%v %v %v", packages, android, e)
}
}
func TestPermissionProbeCleanupAndMissingDirectories(t *testing.T) {
root := t.TempDir()
if e := probeWritable(filepath.Join(root, "new", "cache")); e != nil {
t.Fatal(e)
}
entries, e := os.ReadDir(root)
if e != nil || len(entries) != 0 {
t.Fatalf("probe left files: %v %v", entries, e)
}
writeFixture(t, filepath.Join(root, "file"), "x")
if e := probeWritable(filepath.Join(root, "file", "child")); e == nil {
t.Fatal("file accepted as cache directory")
}
}
func TestPreflightSDKMissingVersusInstalled(t *testing.T) {
s := testServer(t)
work := t.TempDir()
sdk := t.TempDir()
writeFixture(t, filepath.Join(work, "build.gradle"), `android { compileSdk 36 }`)
c := BuildEnvironment{AndroidHome: sdk, GradleUserHome: filepath.Join(t.TempDir(), "cache")}
checkStatus(t, s.preflight(work, c), "platforms;android-36", "warning")
writeFixture(t, filepath.Join(sdk, "platforms", "android-36", "android.jar"), "jar")
checkStatus(t, s.preflight(work, c), "platforms;android-36", "ok")
// A server-local SDK override must be inspected instead of the environment SDK.
missing := filepath.Join(t.TempDir(), "missing-sdk")
writeFixture(t, filepath.Join(work, "local.properties"), "sdk.dir="+strings.ReplaceAll(missing, `\`, `\\`))
checkStatus(t, s.preflight(work, c), "Android SDK", "error")
}
func TestPreflightReadonlySDK(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows uses ACLs rather than Unix chmod")
}
s := testServer(t)
work := t.TempDir()
sdk := t.TempDir()
writeFixture(t, filepath.Join(work, "build.gradle"), `android { compileSdk 36 }`)
os.Chmod(sdk, 0500)
defer os.Chmod(sdk, 0700)
if e := probeWritable(sdk); e == nil {
t.Skip("test identity can bypass Unix write permissions")
}
p := s.preflight(work, BuildEnvironment{AndroidHome: sdk, GradleUserHome: t.TempDir()})
checkStatus(t, p, "SDK 写权限", "warning")
checkStatus(t, p, "platforms;android-36", "error")
}
func TestPreflightFailurePreservesPreviousAPK(t *testing.T) {
s := testServer(t)
p := project(t, s)
cache := filepath.Join(s.root, "cache-file")
writeFixture(t, cache, "not a directory")
writeFixture(t, s.environmentFile, mustJSON(BuildEnvironment{GradleUserHome: cache, Proxy: "direct"}))
apk := filepath.Join(s.work(p), "app", "build", "outputs", "apk", "debug", "old.apk")
writeFixture(t, apk, "old artifact")
b := &Build{ID: id(), Project: p, Status: "queued", Task: ":app:assembleDebug"}
s.builds[b.ID] = b
s.projects[p].Busy = true
s.run(b)
if b.Status != "failed" || b.cmd != nil {
t.Fatalf("executor ran despite preflight failure: %+v", b)
}
if data, e := os.ReadFile(apk); e != nil || string(data) != "old artifact" {
t.Fatal("previous output removed before preflight completed")
}
}