|
| 1 | +package discovery |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/fs" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + "testing/fstest" |
| 8 | + |
| 9 | + "github.com/platformsh/platformify/platformifier" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDiscoverer_discoverBuildSteps(t *testing.T) { |
| 13 | + type fields struct { |
| 14 | + fileSystem fs.FS |
| 15 | + memory map[string]any |
| 16 | + } |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + fields fields |
| 20 | + want []string |
| 21 | + wantErr bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Poetry Django", |
| 25 | + fields: fields{ |
| 26 | + fileSystem: fstest.MapFS{ |
| 27 | + "project/manage.py": &fstest.MapFile{}, |
| 28 | + }, |
| 29 | + memory: map[string]any{ |
| 30 | + "stack": platformifier.Django, |
| 31 | + "type": "python", |
| 32 | + "dependency_managers": []string{"poetry"}, |
| 33 | + "application_root": ".", |
| 34 | + }, |
| 35 | + }, |
| 36 | + want: []string{ |
| 37 | + "# Set PIP_USER to 0 so that Poetry does not complain", |
| 38 | + "export PIP_USER=0", |
| 39 | + "# Install poetry as a global tool", |
| 40 | + "python -m venv /app/.global", |
| 41 | + "pip install poetry==$POETRY_VERSION", |
| 42 | + "poetry install", |
| 43 | + "# Collect static files", |
| 44 | + "poetry run python project/manage.py collectstatic --noinput", |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Pipenv Django with Yarn build", |
| 49 | + fields: fields{ |
| 50 | + fileSystem: fstest.MapFS{ |
| 51 | + "project/manage.py": &fstest.MapFile{}, |
| 52 | + "package.json": &fstest.MapFile{Data: []byte(`{"scripts": {"build": "nuxt build"}}`)}, |
| 53 | + }, |
| 54 | + memory: map[string]any{ |
| 55 | + "stack": platformifier.Django, |
| 56 | + "type": "python", |
| 57 | + "dependency_managers": []string{"poetry", "yarn"}, |
| 58 | + "application_root": ".", |
| 59 | + }, |
| 60 | + }, |
| 61 | + want: []string{ |
| 62 | + "n auto || n lts", |
| 63 | + "hash -r", |
| 64 | + "yarn", |
| 65 | + "yarn build", |
| 66 | + "# Set PIP_USER to 0 so that Poetry does not complain", |
| 67 | + "export PIP_USER=0", |
| 68 | + "# Install poetry as a global tool", |
| 69 | + "python -m venv /app/.global", |
| 70 | + "pip install poetry==$POETRY_VERSION", |
| 71 | + "poetry install", |
| 72 | + "# Collect static files", |
| 73 | + "poetry run python project/manage.py collectstatic --noinput", |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "Next.js without build script", |
| 78 | + fields: fields{ |
| 79 | + fileSystem: fstest.MapFS{}, |
| 80 | + memory: map[string]any{ |
| 81 | + "stack": platformifier.NextJS, |
| 82 | + "type": "nodejs", |
| 83 | + "dependency_managers": []string{"npm"}, |
| 84 | + "application_root": ".", |
| 85 | + }, |
| 86 | + }, |
| 87 | + want: []string{ |
| 88 | + "npm i", |
| 89 | + "npm exec next build", |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | + for _, tt := range tests { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + d := &Discoverer{ |
| 96 | + fileSystem: tt.fields.fileSystem, |
| 97 | + memory: tt.fields.memory, |
| 98 | + } |
| 99 | + got, err := d.discoverBuildSteps() |
| 100 | + if (err != nil) != tt.wantErr { |
| 101 | + t.Errorf("Discoverer.discoverBuildSteps() error = %v, wantErr %v", err, tt.wantErr) |
| 102 | + return |
| 103 | + } |
| 104 | + if !reflect.DeepEqual(got, tt.want) { |
| 105 | + t.Errorf("Discoverer.discoverBuildSteps() = %v, want %v", got, tt.want) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments