-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cross-platform Geb FileInput module support
This resolves an issue on Windows when using the file from `container.copyFileToContainer()` with the Geb FileInput module.
- Loading branch information
Showing
9 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
spock-container-test-app/grails-app/controllers/org/demo/spock/UploadController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.demo.spock | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
class UploadController { | ||
|
||
static allowedMethods = [index: 'GET', store: 'POST'] | ||
|
||
def index() {} | ||
|
||
def store() { | ||
def myFile = request.getFile('myFile') | ||
def text = 'No file uploaded' | ||
if (myFile) { | ||
try (InputStream inputStream = myFile.getInputStream()) { | ||
byte[] bytes = inputStream.readAllBytes() | ||
text = new String(bytes, StandardCharsets.UTF_8) | ||
} | ||
} | ||
render(view: 'store', model: [text: text]) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
spock-container-test-app/grails-app/views/upload/index.gsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%@ page contentType="text/html;charset=UTF-8" %> | ||
<html> | ||
<head> | ||
<title>Upload Test</title> | ||
</head> | ||
<body> | ||
<g:uploadForm action="store"> | ||
<input type="file" name="myFile"> | ||
<input type="submit" value="Upload File"> | ||
</g:uploadForm> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<%@ page contentType="text/html;charset=UTF-8" %> | ||
<html> | ||
<head> | ||
<title>File Uploaded</title> | ||
</head> | ||
<body> | ||
${text} | ||
</body> | ||
</html> |
49 changes: 49 additions & 0 deletions
49
spock-container-test-app/src/integration-test/groovy/org/demo/spock/UploadSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.demo.spock | ||
|
||
import grails.plugin.geb.ContainerGebSpec | ||
import grails.testing.mixin.integration.Integration | ||
import org.demo.spock.pages.UploadPage | ||
import spock.lang.IgnoreIf | ||
import spock.lang.Requires | ||
|
||
@Integration | ||
class UploadSpec extends ContainerGebSpec { | ||
|
||
@Requires({ os.windows }) | ||
void 'should be able to upload files on a Windows host'() { | ||
given: | ||
to UploadPage | ||
|
||
when: | ||
fileInput.file = createFileInputSource( | ||
'src/integration-test/resources/assets/upload-test.txt', | ||
'/tmp/upload-test.txt' | ||
) | ||
|
||
and: | ||
submitBtn.click() | ||
|
||
then: | ||
title == 'File Uploaded' | ||
browser.pageSource.contains('File uploaded successfully') | ||
} | ||
|
||
@IgnoreIf({ os.windows }) | ||
void 'should be able to upload files on a non-Windows host'() { | ||
given: | ||
to UploadPage | ||
|
||
when: | ||
fileInput.file = createFileInputSource( | ||
'src/integration-test/resources/assets/upload-test.txt', | ||
'/tmp/upload-test.txt' | ||
) | ||
|
||
and: | ||
submitBtn.click() | ||
|
||
then: | ||
title == 'File Uploaded' | ||
browser.driver.pageSource.contains('File uploaded successfully') | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spock-container-test-app/src/integration-test/groovy/org/demo/spock/pages/UploadPage.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.demo.spock.pages | ||
|
||
import geb.Page | ||
import geb.module.FileInput | ||
|
||
class UploadPage extends Page { | ||
|
||
static url = '/upload' | ||
static at = { title == 'Upload Test' } | ||
|
||
static content = { | ||
fileInput { $('input', name: 'myFile').module(FileInput) } | ||
submitBtn { $('input', type: 'submit') } | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
spock-container-test-app/src/integration-test/resources/assets/upload-test.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
File uploaded successfully |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/testFixtures/groovy/grails/plugin/geb/support/ContainerGebFileInputSource.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2025 original author or authors | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 grails.plugin.geb.support | ||
|
||
/** | ||
* A File implementation specifically for assigning to the Geb FileInput module when using ContainerGebSpec. | ||
* This will normalize the path for cross-platform compatibility (Windows paths with in Linux containers). | ||
* | ||
* @author Mattias Reichel | ||
* @since 4.2 | ||
*/ | ||
class ContainerGebFileInputSource extends File { | ||
|
||
String normalizedPath | ||
|
||
ContainerGebFileInputSource(String path) { | ||
super(path) | ||
normalizedPath = normalizePath(path) | ||
} | ||
|
||
@Override | ||
String getAbsolutePath() { | ||
return normalizedPath | ||
} | ||
|
||
/** | ||
* Normalizes the path for cross-platform compatibility (Windows paths used in Linux containers). | ||
* | ||
* @param path the original file path | ||
* @return the normalized path with forward slashes | ||
*/ | ||
protected String normalizePath(String path) { | ||
// Replace backslashes with forward slashes for Windows compatibility | ||
path.replaceAll('\\\\', '/') | ||
} | ||
} |