Skip to content

Commit

Permalink
Add a checker for ktfmt for kotlin files (project-chip#27658)
Browse files Browse the repository at this point in the history
* Add a kotlin format workflow

* Update formatting to be bad, to validate we format properly

* Undo format change

* Make a generic kotlin style yaml

* Add format check to kotlin-style

* Update naming for changes when to run kotlin validators

* Add ktfmt download and execution

* Switch ktfmt to google style

* Switch to kotlinlang-style

* Revert files to master, to validate that our kotlin formatter validates format

* Switch to google-style (to have 2 char indents) and apply formatting to all kt files

* Add more exceptions now that code is formatted (some methods became longer)

---------

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
andy31415 and andreilitvin authored Jul 7, 2023
1 parent ddf4db4 commit e532537
Show file tree
Hide file tree
Showing 97 changed files with 2,950 additions and 2,119 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/kotlin-detekt.yaml

This file was deleted.

64 changes: 64 additions & 0 deletions .github/workflows/kotlin-style.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Kotlin

on:
pull_request:
paths:
- "**/*.kt"
- ".github/workflows/kotlin-style.yaml"
- "kotlin-detect-config.yaml"

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}-${{ (github.event_name == 'pull_request' && github.event.number) || (github.event_name == 'workflow_dispatch' && github.run_number) || github.sha }}
cancel-in-progress: true


jobs:
detekt:
name: Static code analysis
runs-on: ubuntu-latest

steps:
- name: "checkout"
uses: actions/checkout@v2

- name: "detekt"
uses: natiginfo/[email protected]
# Detekt seems not to like circular symlinks, so we set up
# explicit paths below
with:
args: --parallel --build-upon-default-config --config kotlin-detect-config.yaml --input examples/android/CHIPTest,examples/android/CHIPTool,examples/java-matter-controller/java,src/controller/java

ktlint:
name: "Format check"
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'

- name: Download ktfmt
run: |
cd /tmp
wget "https://repo1.maven.org/maven2/com/facebook/ktfmt/0.44/ktfmt-0.44-jar-with-dependencies.jar"
- name: Format kotlin files
run: |
find src examples -name '*.kt' \
| xargs java -jar /tmp/ktfmt-0.44-jar-with-dependencies.jar --google-style
- name: Ensure git works in current working directory
run: git config --global --add safe.directory `pwd`

- name: Check for uncommited changes
run: |
git add .
# Show the full diff
git diff-index -p HEAD --
# Also show just the files that are different, to make it easy
# to tell at a glance what might be going on. And throw in
# --exit-code to make this job fail if there is a difference.
git diff-index --exit-code HEAD --
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package com.tcl.chip.chiptest

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.tcl.chip.chiptest", appContext.packageName)
}
}
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.tcl.chip.chiptest", appContext.packageName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,60 @@ import androidx.appcompat.app.AppCompatActivity
import chip.platform.*
import com.tcl.chip.chiptest.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {

private val msgShowLog = 1
private val msgShowLog = 1

private lateinit var binding: ActivityMainBinding
private val mainHandler: Handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
when (msg.what) {
msgShowLog -> {
binding.testLog.append(msg.obj.toString())
}
}
private lateinit var binding: ActivityMainBinding
private val mainHandler: Handler =
object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
when (msg.what) {
msgShowLog -> {
binding.testLog.append(msg.obj.toString())
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.testLog.movementMethod = ScrollingMovementMethod();
TestEngine.setListener { log ->
var msg = mainHandler.obtainMessage(msgShowLog, log)
mainHandler.sendMessage(msg)
}
binding.testLog.movementMethod = ScrollingMovementMethod()
TestEngine.setListener { log ->
var msg = mainHandler.obtainMessage(msgShowLog, log)
mainHandler.sendMessage(msg)
}

binding.runButton.setOnClickListener {
binding.testLog.text = ""
Thread{
var ret = TestEngine.runTest();

var log:String = if (ret == 0) {
getString(R.string.test_finished)
} else {
getString(R.string.test_failed,ret)
}
var msg = mainHandler.obtainMessage(msgShowLog, log)
mainHandler.sendMessage(msg)
}.start()
}
binding.runButton.setOnClickListener {
binding.testLog.text = ""
Thread {
var ret = TestEngine.runTest()

AndroidChipPlatform(AndroidBleManager(), PreferencesKeyValueStoreManager(this), PreferencesConfigurationManager(this), NsdManagerServiceResolver(this), NsdManagerServiceBrowser(this), ChipMdnsCallbackImpl(), DiagnosticDataProviderImpl(this))
var log: String =
if (ret == 0) {
getString(R.string.test_finished)
} else {
getString(R.string.test_failed, ret)
}
var msg = mainHandler.obtainMessage(msgShowLog, log)
mainHandler.sendMessage(msg)
}
.start()
}

AndroidChipPlatform(
AndroidBleManager(),
PreferencesKeyValueStoreManager(this),
PreferencesConfigurationManager(this),
NsdManagerServiceResolver(this),
NsdManagerServiceBrowser(this),
ChipMdnsCallbackImpl(),
DiagnosticDataProviderImpl(this)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.tcl.chip.chiptest

import org.junit.Test

import org.junit.Assert.*
import org.junit.Test

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ class CHIPToolActivity :
if (savedInstanceState == null) {
val fragment = SelectActionFragment.newInstance()
supportFragmentManager
.beginTransaction()
.add(R.id.nav_host_fragment, fragment, fragment.javaClass.simpleName)
.commit()
.beginTransaction()
.add(R.id.nav_host_fragment, fragment, fragment.javaClass.simpleName)
.commit()
} else {
networkType =
ProvisionNetworkType.fromName(savedInstanceState.getString(ARG_PROVISION_NETWORK_TYPE))
ProvisionNetworkType.fromName(savedInstanceState.getString(ARG_PROVISION_NETWORK_TYPE))
}

if (intent?.action == NfcAdapter.ACTION_NDEF_DISCOVERED)
onNfcIntent(intent)
if (intent?.action == NfcAdapter.ACTION_NDEF_DISCOVERED) onNfcIntent(intent)

if (Intent.ACTION_VIEW == intent?.action) {
onReturnIntent(intent)
Expand Down Expand Up @@ -97,10 +96,8 @@ class CHIPToolActivity :

override fun onCommissioningComplete(code: Int) {
runOnUiThread {
Toast.makeText(
this,
getString(R.string.commissioning_completed, code),
Toast.LENGTH_SHORT).show()
Toast.makeText(this, getString(R.string.commissioning_completed, code), Toast.LENGTH_SHORT)
.show()
}
ChipClient.getDeviceController(this).close()
showFragment(SelectActionFragment.newInstance(), false)
Expand Down Expand Up @@ -128,7 +125,8 @@ class CHIPToolActivity :
}

private fun showFragment(fragment: Fragment, showOnBack: Boolean = true) {
val fragmentTransaction = supportFragmentManager
val fragmentTransaction =
supportFragmentManager
.beginTransaction()
.replace(R.id.nav_host_fragment, fragment, fragment.javaClass.simpleName)

Expand All @@ -153,8 +151,7 @@ class CHIPToolActivity :

lateinit var setupPayload: OnboardingPayload
try {
setupPayload =
OnboardingPayloadParser().parseQrCode(uri.toString().toUpperCase())
setupPayload = OnboardingPayloadParser().parseQrCode(uri.toString().toUpperCase())
} catch (ex: UnrecognizedQrCodeException) {
Log.e(TAG, "Unrecognized QR Code", ex)
Toast.makeText(this, "Unrecognized QR Code", Toast.LENGTH_SHORT).show()
Expand All @@ -163,23 +160,26 @@ class CHIPToolActivity :

val deviceInfo = CHIPDeviceInfo.fromSetupPayload(setupPayload)

val buttons = arrayOf(
val buttons =
arrayOf(
getString(R.string.nfc_tag_action_show),
getString(R.string.nfc_tag_action_wifi),
getString(R.string.nfc_tag_action_thread))
getString(R.string.nfc_tag_action_thread)
)

AlertDialog.Builder(this)
.setTitle(R.string.nfc_tag_action_title)
.setItems(buttons) { _, which ->
this.networkType = when (which) {
.setTitle(R.string.nfc_tag_action_title)
.setItems(buttons) { _, which ->
this.networkType =
when (which) {
1 -> ProvisionNetworkType.WIFI
2 -> ProvisionNetworkType.THREAD
else -> null
}
onCHIPDeviceInfoReceived(deviceInfo)
}
.create()
.show()
onCHIPDeviceInfoReceived(deviceInfo)
}
.create()
.show()
}

private fun onReturnIntent(intent: Intent) {
Expand Down Expand Up @@ -222,18 +222,13 @@ class CHIPToolActivity :
setupPayload.setupPinCode = payload.getLong("setupPinCode")

val deviceInfo = CHIPDeviceInfo.fromSetupPayload(setupPayload)
val buttons = arrayOf(
getString(R.string.nfc_tag_action_show)
)
val buttons = arrayOf(getString(R.string.nfc_tag_action_show))

AlertDialog.Builder(this)
.setTitle(R.string.provision_custom_flow_alert_title)
.setItems(buttons) { _, _ ->
onCHIPDeviceInfoReceived(deviceInfo)
}
.setItems(buttons) { _, _ -> onCHIPDeviceInfoReceived(deviceInfo) }
.create()
.show()

} catch (ex: UnrecognizedQrCodeException) {
Log.e(TAG, "Unrecognized Payload", ex)
Toast.makeText(this, "Unrecognized Setup Payload", Toast.LENGTH_SHORT).show()
Expand Down
Loading

0 comments on commit e532537

Please sign in to comment.