Skip to content

Commit

Permalink
Bug fix: wrong file name if group value has whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 committed Sep 12, 2021
1 parent 9f76ba5 commit 73a3783
Show file tree
Hide file tree
Showing 28 changed files with 113 additions and 25 deletions.
10 changes: 2 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@ end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
max_line_length = 120

[*.md]
trim_trailing_whitespace = false
max_line_length = 120

[*.{yml, yaml}]
indent_size = 2

[*.blade.php]
trim_trailing_whitespace = false

[*.php]
ij_php_align_key_value_pairs = false
ij_php_comma_after_last_array_element = true
Expand All @@ -27,4 +21,4 @@ ij_php_keep_blank_lines_in_code = 1
ij_php_concat_spaces = false
ij_php_blank_lines_before_return_statement = 1
ij_php_space_before_short_closure_left_parenthesis = true
ij_php_anonymous_brace_style = next_line
ij_php_anonymous_brace_style = next_line
2 changes: 1 addition & 1 deletion src/Exporters/JsonLanguagesExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function handle(LarexExportCommand $command, CsvReader $reader): int

$data = [];
foreach ($groups as $group => $keys) {
foreach ($keys as $key=>$value) {
foreach ($keys as $key => $value) {
$data["$group.$key"] = $value;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Support/CsvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function parse(bool $skipEmpty = true): array
foreach ($rows as $columns) {

//get first two columns values
$group = $columns->get('group');
$key = $columns->get('key');
$group = trim($columns->get('group'));
$key = trim($columns->get('key'));

//check if key is filled
if ($key === '') {
Expand All @@ -59,6 +59,7 @@ public function parse(bool $skipEmpty = true): array

//loop language columns
foreach ($columns->skip(2) as $lang => $value) {
$lang = trim($lang);
$item = $value ?? '';

if ($item === '') {
Expand Down
5 changes: 3 additions & 2 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ public static function bytesToHuman($bytes): string
*/
public static function putJson(string $path, $data): void
{
File::put($path, json_encode($data,
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$content = json_encode($data,
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
File::put($path, Str::finish($content, "\n"));
}
}
20 changes: 20 additions & 0 deletions tests/Exporters/JsonGroupsExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,23 @@
->expectsOutput('No entries exported.')
->assertExitCode(0);
});

it('exports strings trimming whitespaces in group and key', function () {
initFromStub('exporters.json-groups.spaces.input');

$this->artisan(LarexExportCommand::class, ['exporter' => 'json:group'])
->expectsOutput(sprintf("Processing the '%s' file...", localizationPath(true)))
->expectsOutput('resources/lang/en/app.json created successfully.')
->expectsOutput('resources/lang/it/app.json created successfully.')
->assertExitCode(0);

expect(resource_path('lang/en/app.json'))
->toBeFile()
->fileContent()
->toEqualStub('exporters.json-groups.spaces.output-en-app');

expect(resource_path('lang/it/app.json'))
->toBeFile()
->fileContent()
->toEqualStub('exporters.json-groups.spaces.output-it-app');
});
20 changes: 20 additions & 0 deletions tests/Exporters/JsonLanguagesExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,23 @@
->expectsOutput('No entries exported.')
->assertExitCode(0);
});

it('exports strings trimming whitespaces in group and key', function () {
initFromStub('exporters.json-language.spaces.input');

$this->artisan(LarexExportCommand::class, ['exporter' => 'json:lang'])
->expectsOutput(sprintf("Processing the '%s' file...", localizationPath(true)))
->expectsOutput('resources/lang/en.json created successfully.')
->expectsOutput('resources/lang/it.json created successfully.')
->assertExitCode(0);

expect(resource_path('lang/en.json'))
->toBeFile()
->fileContent()
->toEqualStub('exporters.json-language.spaces.output-en');

expect(resource_path('lang/it.json'))
->toBeFile()
->fileContent()
->toEqualStub('exporters.json-language.spaces.output-it');
});
20 changes: 20 additions & 0 deletions tests/Exporters/LaravelExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,23 @@
->fileContent()
->toEqualStub('exporters.laravel.base.output-it-special');
});

it('exports strings trimming whitespaces in group and key', function () {
initFromStub('exporters.laravel.spaces.input');

$this->artisan(LarexExportCommand::class, ['exporter' => 'laravel'])
->expectsOutput(sprintf("Processing the '%s' file...", localizationPath(true)))
->expectsOutput('resources/lang/en/app.php created successfully.')
->expectsOutput('resources/lang/it/app.php created successfully.')
->assertExitCode(0);

expect(resource_path('lang/en/app.php'))
->toBeFile()
->fileContent()
->toEqualStub('exporters.laravel.spaces.output-en');

expect(resource_path('lang/it/app.php'))
->toBeFile()
->fileContent()
->toEqualStub('exporters.laravel.spaces.output-it');
});
2 changes: 1 addition & 1 deletion tests/Stubs/exporters/json-groups/base/output-en-app.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"first": "First",
"second": "Second",
"third": "Third"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"numeric.3": "March",
"space.escape": "nope",
"space.noescape": "nope"
}
}
2 changes: 1 addition & 1 deletion tests/Stubs/exporters/json-groups/base/output-it-app.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"first": "Primo",
"second": "Secondo",
"third": "Terzo"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"numeric.3": "Marzo",
"space.escape": " ",
"space.noescape": " "
}
}
2 changes: 2 additions & 0 deletions tests/Stubs/exporters/json-groups/spaces/input.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group,key,en,it
app , first , First , Primo
3 changes: 3 additions & 0 deletions tests/Stubs/exporters/json-groups/spaces/output-en-app.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"first": " First "
}
3 changes: 3 additions & 0 deletions tests/Stubs/exporters/json-groups/spaces/output-it-app.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"first": " Primo "
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"first": "First"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"first": "Primo"
}
}
2 changes: 1 addition & 1 deletion tests/Stubs/exporters/json-language/base/output-en.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"numeric.months.3": "March",
"space.escape.yes": "nope",
"space.escape.no": "nope"
}
}
2 changes: 1 addition & 1 deletion tests/Stubs/exporters/json-language/base/output-it.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"numeric.months.3": "Marzo",
"space.escape.yes": " ",
"space.escape.no": " "
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"another.third": "Third",
"sub.multi.a": "A",
"sub.multi.b": "B"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"another.third": "Terzo",
"sub.multi.a": "a",
"sub.multi.b": "b"
}
}
2 changes: 2 additions & 0 deletions tests/Stubs/exporters/json-language/spaces/input.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group,key,en,it
app , first , First , Primo
3 changes: 3 additions & 0 deletions tests/Stubs/exporters/json-language/spaces/output-en.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app.first": " First "
}
3 changes: 3 additions & 0 deletions tests/Stubs/exporters/json-language/spaces/output-it.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app.first": " Primo "
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"another.third": "Third",
"sub.multi.a": "A",
"sub.multi.b": "B"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"another.third": "Terzo",
"sub.multi.a": "a",
"sub.multi.b": "b"
}
}
2 changes: 2 additions & 0 deletions tests/Stubs/exporters/laravel/spaces/input.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group,key,en,it
app , first , First , Primo
7 changes: 7 additions & 0 deletions tests/Stubs/exporters/laravel/spaces/output-en.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'first' => ' First ',

];
7 changes: 7 additions & 0 deletions tests/Stubs/exporters/laravel/spaces/output-it.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'first' => ' Primo ',

];

0 comments on commit 73a3783

Please sign in to comment.