Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Align access bits across temp and regular files to avoid access related issues #5379

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.SystemUtils;
import org.vividus.util.ResourceUtils;
import org.vividus.util.json.JsonUtils;
import org.vividus.util.property.PropertyMappedCollection;
Expand Down Expand Up @@ -58,6 +61,18 @@ public CustomTranslationsPlugin(PropertyMappedCollection<Map<String, ?>> customT
Path indexJs = ResourceUtils.createTempFile("index", ".js");
Files.write(indexJs, jsFileLines);

/**
* For UNIX like operation systems default access for temp files is 600, whereas for regular files the
* default access is 644, so the following fix is used to align access bits across all files being created
* during test execution and avoid potential access related issues.
*/
if (SystemUtils.IS_OS_UNIX)
{
Files.setPosixFilePermissions(indexJs,
Set.of(PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_READ,
PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ));
}

this.pluginFiles.put(INDEX_JS, Path.of(indexJs.toUri()));
getConfig().setJsFiles(List.of(INDEX_JS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.vividus.util.json.JsonUtils;
import org.vividus.util.property.PropertyMappedCollection;

Expand All @@ -31,20 +35,18 @@ class CustomTranslationsPluginTests
@Test
void shouldGenerateIndexJs() throws IOException
{
var plugin = new CustomTranslationsPlugin(new PropertyMappedCollection<>(Map.of("en", Map.of(
"tab", Map.of(
"suites", Map.of(
"name", "Batches Tab"
)
)
))), new JsonUtils());
var pluginFiles = plugin.getPluginFiles();
assertEquals(1, pluginFiles.size());
var indexJsPath = pluginFiles.get("index.js");
assertEquals("""
'use strict';
allure.api.addTranslation('en', {"tab":{"suites":{"name":"Batches Tab"}}});
""".replaceAll("\r\n|\n", System.lineSeparator()), Files.readString(indexJsPath));
""".replaceAll("\r\n|\n", System.lineSeparator()), Files.readString(generateIndexJs()));
}

@Test
@DisabledOnOs(OS.WINDOWS)
void shouldGenerateIndexJsWith644Permissions() throws IOException
{
var permissions = PosixFilePermissions.toString(Files.getPosixFilePermissions(generateIndexJs()));
assertEquals("rw-r--r--", permissions);
}

@Test
Expand All @@ -53,4 +55,18 @@ void shouldNotGenerateIndexJsIfNoTranslationsAreProvided() throws IOException
var plugin = new CustomTranslationsPlugin(new PropertyMappedCollection<>(Map.of()), new JsonUtils());
assertEquals(0, plugin.getPluginFiles().size());
}

private Path generateIndexJs() throws IOException
{
var plugin = new CustomTranslationsPlugin(new PropertyMappedCollection<>(Map.of("en", Map.of(
"tab", Map.of(
"suites", Map.of(
"name", "Batches Tab"
)
)
))), new JsonUtils());
var pluginFiles = plugin.getPluginFiles();
assertEquals(1, pluginFiles.size());
return pluginFiles.get("index.js");
}
}
Loading