Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

#305 fix config file checker #532

Merged
merged 5 commits into from
Feb 14, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ dist
jdeploy-bundle
node_modules
.DS_Store
*.patch
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ public static void init(Actions actions, PropertiesBuilders propertiesBuilders)

@Override
public final void run() {
Outputter out = new PicocliOutputter(System.out, isAnsi());
P properties = this.getProperties(propertiesBuilders, out);

List<String> errors = checkOptions();
if (errors != null && !errors.isEmpty()) {
String errorsInOne = errors.stream()
.map(error -> String.format(RESOURCE_BUNDLE.getString("message.item_list"), error))
.collect(Collectors.joining("\n"));
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.params_are_invalid") + "\n" + errorsInOne);
}
Outputter out = new PicocliOutputter(System.out, isAnsi());
NewAction<P, C> action = this.getAction(actions);
P properties = this.getProperties(propertiesBuilders, out);
C client = this.getClient(properties);
action.act(out, properties, client);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/crowdin/cli/properties/BaseProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
Expand Down Expand Up @@ -85,7 +86,12 @@ public void populateWithDefaultValues(BaseProperties props) {
@Override
public PropertiesBuilder.Messages checkProperties(@NonNull BaseProperties props, CheckType checkType) {
PropertiesBuilder.Messages messages = new PropertiesBuilder.Messages();

if (props.getApiToken() == null) {
String confFilePath = props.getConfigFilePath();
if (confFilePath == null || !(new File(confFilePath).exists())) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.configuration_file_not_exist"));
}
}
if (StringUtils.isEmpty(props.getApiToken())) {
messages.addError(RESOURCE_BUNDLE.getString("error.config.missed_api_token"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.File;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -164,6 +165,22 @@ public void testBuildNoConfigFileTargets() {
assertThrows(NullPointerException.class, () -> propertiesBuilders.buildPropertiesWithTargets(out, null, null, okParams));
}

@Test
public void testBuildNoConfigFileAndNoToken() {
ParamsWithFiles params = new ParamsWithFiles() {{
setBasePathParam(null);
setTokenParam(null);
setSourceParam(Utils.regexPath(Utils.normalizePath("/hello/world")));
setTranslationParam("/hello/%two_letters_code%/%file_name%.%file_extension%");
}};

Exception actualException = assertThrows(RuntimeException.class, () ->
propertiesBuilders.buildPropertiesWithFiles(out, null, null, params));

assertEquals(RESOURCE_BUNDLE.getString("error.configuration_file_not_exist"), actualException.getMessage());

}

@Test
public void testPropertiesWithTarget() {
File configFile = new File("folder/crowdinTest.yml");
Expand Down